0

I have a background service which runs in background to track screen on and off events. its started as below :

Intent intentService = new Intent(context, UnlockdService.class);
context.startService(intentService);

And it is returning Service.START_REDELIVER_INTENT in the method onStartCommand. But still when android system kills app this thread dies as well.

How can I prevent it?

Kruti Mevada
  • 517
  • 5
  • 7
  • 21
  • There is no way you can assure that a service won't be killed. The best you can do is make sure you're restarted when possible. – Gabe Sechan Mar 04 '16 at 00:28

2 Answers2

0

I think it's a better solution to have a BroadcastReceiver instead of a Service. In your onReceive, you could then start your service to do its work, and then the service could self terminate - maybe you could use an IntentService if your work is one shot.

If you still want a Service, you can make it less likely to be killed by having a permanent notification in the notification bar, but that can be bad from a user perspective. I strongly recommend going the broadcast receiver route.

Francesc
  • 25,014
  • 10
  • 66
  • 84
0

The solution was so simple: Returning START_STICKY from onStartCommand() did the job.

Floern
  • 33,559
  • 24
  • 104
  • 119
Kruti Mevada
  • 517
  • 5
  • 7
  • 21