onTaskRemoved not getting called if the activity started by the service is removed

royatirek :

I am using oreo 8.1.0. I am facing a weird problem whose solution I can't find on stackoverflow, hence I am writing this question. I know one solution that through foreground service, I can implement it but I don't find notification user friendly in my application context.

I will describe my problem using two cases.

Case 1: When user opens my app by clicking on the icon and removes it from recent apps, then service automatically restarts. This is fine.

Case 2: Here my app is closed and is not in recent apps. When user copies a text, then my service starts one of the activity of my app but when he removes it from the recent apps, then my service gets stopped permanently.

So my problem lies in second case,I don't want my service to get killed. Even if it gets killed I want it to restart.

I tried all the methods mentioned on the stackoverflow like using START_STICKY and onTaskRemoved but I am not able to make it work.

Even I tried killing my activity whenever user clicks on on recent app button and remove it from the recent apps programmatically but this also did not work. Though this restarts the service even in second case when user kills my app using the back button.

This part of the code is from the activity which opens when user copies some text.

@Override
public void finish() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        super.finishAndRemoveTask();
    } else {
        super.finish();
    }

}

This part of the code is from the service that starts the activity.

@Override
public void onTaskRemoved(Intent rootIntent){
    Log.d("testing 12","onTaskRemoved");
    Intent restartServiceTask = new Intent(getApplicationContext(),CBWatcherService.class);
    restartServiceTask.setPackage(getPackageName());
    PendingIntent restartPendingIntent =PendingIntent.getService(getApplicationContext(), 1,restartServiceTask, PendingIntent.FLAG_ONE_SHOT);
    AlarmManager myAlarmService = (AlarmManager) getApplicationContext().getSystemService(this.ALARM_SERVICE);
    myAlarmService.set(
            AlarmManager.ELAPSED_REALTIME,
            SystemClock.elapsedRealtime() + 1000,
            restartPendingIntent);

    super.onTaskRemoved(rootIntent);
}

@Override
public int onStartCommand(Intent intent,int flag,int startId){
    super.onStartCommand(intent, flag, startId);
    return START_STICKY;

}

EDIT 1
I just need the service to remain alive.Here are the things that will not hinder the user experience in my app case
1. Killing the activity by yourself programitically when onPause is called so that service does not get killed is acceptable if you make it happen
2. Showing the notification for few second is acceptable
3. Restarting the service is acceptable

royatirek :

This was the problem Whenever my service used to start the activity, my service used to get destroyed after the activity used to get started.

Why onTaskRemoved was not working?
The thing I was doing was not working because my service used to get destroyed and hence it was not active to listen to onTaskRemoved.

How I solved it?
The solution to this problem was putting a check in the activity(started by service) to check whether the service is alive or not. As in my case service was getting destroyed. Hence I need to start the service again if the service is not alive.
It even fits the OREO design pattern as we can start the background service when the app is in foreground and the service will stay alive even if the activity gets destroyed.

On Oreo though after sometime service gets destroyed but this is a different problem.

More Info

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=37020&siteId=1