I was wondering if it is possible to set the priority of an IntentServ开发者_高级运维ice
like you can with a Thread
. So far I have not found anything.
You can set Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND)
in the onHandleIntent()
method of your intent service.
The solution by CommonsWare also works. This one is simpler, though.
The HandlerThread
that IntentService
uses is not exposed to the SDK. It is set to Process.THREAD_PRIORITY_DEFAULT
as a priority.
Note that IntentService
is 143 lines of code, including whitespace and comments, so you might consider just cloning it to have one with the priority you seek.
Just to make it clear - by default IntentService
priority is default- Process.THREAD_PRIORITY_DEFAULT
because it uses HandlerThread
Internally. You can use Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND)
in OnHandleIntent(), or whatever the CommonsWare suggested in the answer.
some people raised this as bug. and there is also patch available for this.
I changed the IntentService's priority using: "android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);" in the constructor of the IntentService class.
In my case it was changed from: "Process.THREAD_PRIORITY_DEFAULT" which is zero (0) to "Process.THREAD_PRIORITY_BACKGROUND" which is ten (10) and the result was visible.
精彩评论