I have created a simple service and calling it from an activity with startService(intent). I am returning START_STICKY from my service's onStartCommand(). I want the service to keep running even if the activity is closed or removed from task list. When I test on emulator it is working fine - service is restarted after a second if I remove the application from recent screen list (by swiping or clear all). But on real device (running on Android 5.1) the service is killed but not started. I can see the following message.
04-22 20:26:26.436 714-1454/? W/ActivityManager: Scheduling restart of crashed service com.example.xxx.MyPkg/.MyService in 1000ms
04-22 20:26:26.441 714-1454/? I/ActivityManager: Force stopping service ServiceRecord{9df8b23 u0 com.example.xxx.MyPkg/.MyService}
Also, at some other point I have scheduled a start of the same service using Alarmmanager and PendingIntent. If the application is there in recent list, then only the service start works through Alarm, else not. Again, on emulator it is working fine.
I read fews questions and answers which says that START_STICKY has some issues in Kitkat and JellyBean. There were suggestions of scheduling a restart after few seconds from service's onTaskRemoved(). But firstly, I don't see onTaskRemoved() getting called on all clearing actions. And secondly, given that scheduled service start is not working when app is not present in task list, I am not sure it will work even if I schedule from onTaskRemoved() or activity's onDestroy().
Is there any way to get around this issue?