问题
I was wondering if it is possible to set the priority of an IntentService
like you can with a Thread
. So far I have not found anything.
回答1:
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.
回答2:
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.
回答3:
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.
回答4:
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.
来源:https://stackoverflow.com/questions/3621749/how-to-set-priority-of-intentservice-in-android