I hope title itself says what's my question is.
I have a Service
which runs in foreground.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
new Intent(this, WidgetFlashReceiver.class)
.setAction(Constants.ACTION_NOTIFICATION_CLICK),
PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(pendingIntent);
builder.setOngoing(true);
builder.setAutoCancel(false);
builder.setContentTitle("Sample Service");
Notification notification = builder.build();
notification.icon = R.drawable.ic_launcher;
notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
startForeground(NOTIFICATION_ID, notification);
return START_STICKY;
}
but this Service getting killed when app is swiped off from recent tasks even though it is a foreground Service.
Any help is appreciated.
use the service onTaskRemoved() method.
Looking at this comment about the Android bug, I found this solution that solve the problem for me!
add below code in your manifest file
There's a bug in android that describes the same issue you're facing: https://code.google.com/p/android/issues/detail?id=53313
Also, there's a lot of info on the matter in SO:
Foreground service being killed by Android
Service being killed while holding wake lock and after calling startForeground