My Solution to write sticky service is working fine below Android version Lollipop but above Lollipop version service is getting closed when user kills the application from background.
Any suggestion guys.
My Solution to write sticky service is working fine below Android version Lollipop but above Lollipop version service is getting closed when user kills the application from background.
Any suggestion guys.
Use can start service as Foreground even os can't kill it for more info check linkRunning a service in the foreground
I got solution about it not why it's work for me but try this.
Override the onTaskRemoved()
@Override
public void onTaskRemoved(Intent rootIntent) {
// TODO Auto-generated method stub
//Write your stuff which you set in onDestroy()
}
and set same code which you set in onDestroy() it work for me.
For instance below code will work on Lollipop.
@Override
public void onTaskRemoved(Intent rootIntent) {
Intent restartService = new Intent(getApplicationContext(),
this.getClass());
restartService.setPackage(getPackageName());
PendingIntent restartServicePI = PendingIntent.getService(
getApplicationContext(), 1, restartService,
PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 1000, restartServicePI);
}
But Things are getting quite tricky Since android N release. And now android O put some more restrictions on background tasks. So the thing in In Android O no background service can run the service will kill by OS just after your application gone from the foreground(Only Foreground services can run i.e Service with a notification on top). Here is a informative solution.