Skip to content Skip to sidebar Skip to footer

Broadcast Receiver Not Calling Service While App Was Killed

My target was Restarting the Service when app is in background or even killed from home page by sweeping. App & Service is working nice while app is in foreground and backgroun

Solution 1:

You should restart Service in onTaskRemoved().

@OverridepublicvoidonTaskRemoved(Intent rootIntent) {
        IntentrestartService=newIntent(getApplicationContext(),
                this.getClass());
        restartService.setPackage(getPackageName());
        PendingIntentrestartServicePI= PendingIntent.getService(
                getApplicationContext(), 1, restartService,
                PendingIntent.FLAG_ONE_SHOT);
        AlarmManageralarmService= (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 1000, restartServicePI);
    }

NOTE:- Starting from android O . You can not call startService.

The startService() method now throws an IllegalStateException if an app targeting Android 8.0 tries to use that method in a situation when it isn't permitted to create background services.

This does not apply to foreground services, which are noticeable to the user. It can run in background with a notification on top. By default, these restrictions only apply to apps that target Android 8.0 (API level 26) or higher. However, users can enable most of these restrictions for any app from the Settings screen, even if the app targets an API level lower than 26. So in case if user enables the restrictions for below API 26 your Service will not work. Read Background Execution Limits.

So Try to avoid using Service if you can . Make use of WorkManager if it fits the requirements.

Post a Comment for "Broadcast Receiver Not Calling Service While App Was Killed"