android run background service on startup

www.l‮ruttua‬i.com
android run background service on startup

run a background service when android start up:

AndroidManifest.xml:

<receiver android:name=".BootBroadcastReceiver" >   
    <intent-filter>   
        <action android:name="android.intent.action.BOOT_COMPLETED" />   
    </intent-filter>   
</receiver>

request permission (AndroidManifest.xml)

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED">
</uses-permission>

run a background service at boot time

public class BootBroadcastReceiver extends BroadcastReceiver {     
    static final String ACTION = "android.intent.action.BOOT_COMPLETED";   
    @Override   
    public void onReceive(Context context, Intent intent) {   
        // BOOT_COMPLETED” start Service    
        if (intent.getAction().equals(ACTION)) {   
            //Service    
            Intent serviceIntent = new Intent(context, StartOnBootService.class);       
            context.startService(serviceIntent);   
        }   
    }    
}
Created Time:2017-08-31 08:45:13  Author:lautturi