android_beginner
android_beginner

Reputation: 143

Prevent a service from being killed

How to prevent a Service which should run without showing a notification from being killed even if the app is killed?

I'm trying to run a Service which tracks GPS location after every 10 minutes and I want my Service to run continuously even if the app is killed. I've already tried startForeground() but it also gives me a notification. How can I get a solution without notification?

Upvotes: 3

Views: 1808

Answers (3)

android_beginner
android_beginner

Reputation: 143

Try to use START_STICKY or apply wake lock which calls the service immediately as soon as it stops.

Upvotes: 0

1234567
1234567

Reputation: 2485

Add START_STICKY.
This causes service to restart.

Upvotes: 1

Bharath Mg
Bharath Mg

Reputation: 1127

You can return START_REDELIVER_INTENT from your Service onStartCommand. So when your service is killed, the service will be restarted and last delivered intent will be sent.

Also you can return START_STICKY to auto-restart the service killed by android.

So if even user swipes close the app from open apps screen, your service will restart.

Ref http://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent,int,int)

Upvotes: 1

Related Questions