How to set Priority of IntentService in Android

怎甘沉沦 提交于 2019-12-09 17:02:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!