How to set Priority of IntentService in Android

前端 未结 4 1256
再見小時候
再見小時候 2021-01-05 06:41

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.

相关标签:
4条回答
  • 2021-01-05 07:25

    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.

    0 讨论(0)
  • 2021-01-05 07:36

    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.

    0 讨论(0)
  • 2021-01-05 07:38

    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.

    0 讨论(0)
  • 2021-01-05 07:44

    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.

    0 讨论(0)
提交回复
热议问题