doga
doga

Reputation: 491

How to set the priority of android service?

Is there any way to set the priority of android service? I want to run the service in background after destroying App's main actvity. So i am returning "START_REDELIVERY_INTENT" from my service. But restarting of service is taking some time (around 1-3 minutes). I want to restart the service instantly. Is there any way to set the priority of android service.

FYI, I am creating service using startService and passing an object through intent.putExtra().

Below snapshot is taken from setting/apps/running apps by my android device, which shows that app is restarting... My app is real time specific, so i want to restart it instantly...

enter image description here

Upvotes: 3

Views: 18783

Answers (3)

waqaslam
waqaslam

Reputation: 68167

You cannot control this behavior of Service as it depends on the OS and available resources. However, for a continuously running background service, I would suggest you to return START_STICKY - which tries its best to re-run the service once its destroyed due to any reasons.

Upvotes: 3

Kiril Aleksandrov
Kiril Aleksandrov

Reputation: 2591

The priority depends on the component type and the current app that the user is using. The highest priority have the current Activity that is displayed to the user and all foreground services. Foreground services must have a notification that cannot be dismissed. To create a foreground service you have to call startForeground(...) method from the Service itself passing the id of the notification and the Notification object itself. You can use the id if you want to update the notification later.

I recommend you to make the service bound and bind your activity to the service. This way the you can start and stop the service in your onResume and onPause methods.

Upvotes: 3

sergej shafarenka
sergej shafarenka

Reputation: 20406

The only way to increase service priority in this case is to make it a foreground service.

Upvotes: 2

Related Questions