Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

High priority Android services

Tags:

android

I'm looking into implementing an over-Bluetooth video streaming service for Android.

How can I ensure that:

  • the service runs with a high priority? I'd like to minimize latency.
  • the service is low of the list of things to be killed when memory is tight?
like image 826
user48956 Avatar asked Sep 13 '11 20:09

user48956


2 Answers

There are two important things you need to do to make sure your service keeps running:

  1. Make sure you call startService() instead of (or in addition to) binding. This will make sure that the service continues running, even if the Activity that created it is killed.

  2. Use startForegroud() to run the service in the foreground, so that Android won't reclaim it when memory is needed. It requires that you also create a Notification that gets published when the service actually starts, putting an icon in the notification area and letting the user know your service is still running.

For more information:

http://developer.android.com/reference/android/app/Service.html http://developer.android.com/guide/topics/fundamentals/bound-services.html

like image 200
derekerdmann Avatar answered Nov 19 '22 15:11

derekerdmann


If you are/were in the same situation as me, being forced to use bindService(), the solution was to set the

Context.BIND_IMPORTANT

flag in that call. That raises the service's priority to that of the binding app, which if it is the foreground app, is the highest possible. If you don't use that flag, the service needs to fight over a measly 10% of resources with all the other background services. (the other 90% are reserved for the foreground app).

Note that manually setting the service's thread priority, without setting this flag during bindService(), makes no difference.

like image 43
Doktor Schrott Avatar answered Nov 19 '22 15:11

Doktor Schrott