简体   繁体   中英

How to set Priority of IntentService in Android

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM