How to set the priority of android service?

15,048

Solution 1

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.

Solution 2

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.

Solution 3

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

Share:
15,048
doga
Author by

doga

Updated on June 16, 2022

Comments

  • doga
    doga about 2 years

    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