Android Starting Service At Boot Time , How to Restart Service Class After Device Reboot

Android Starting Service at Boot Time , How to restart service class after device Reboot?

Create a BroadcastReceiver and register it to receive ACTION_BOOT_COMPLETED. You also need RECEIVE_BOOT_COMPLETED permission.

Read: Listening For and Broadcasting Global Messages, and Setting Alarms

Restart service on device reboot

Now I fixed in on my own it was easier then i thought

public class BootCompletedIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

Intent i=new Intent(context, YourClass.class);

if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {
context.startForegroundService(i);
}
else {
context.startService(i);
}

}


Related Topics



Leave a reply



Submit