Can't do heavy work on broadcast receiver which is inside the Service

前端 未结 1 814
野性不改
野性不改 2020-12-07 05:28

I created a Broadcast Receiver Which is inside the Service Class (Because of the Android Broadcast receiver restrictions Broadca

相关标签:
1条回答
  • 2020-12-07 06:30

    From documentation: A service runs in the main thread of its hosting process; the service does not create its own thread and does not run in a separate process unless you specify otherwise. You should run any blocking operations on a separate thread within the service to avoid Application Not Responding (ANR) errors. I think that's the reason why you can not do "heavy" work within Service class. I didn't go through your code, but you can do two things:

    1.) Make new thread inside service on create and do "heavy work"(don' forget to stop it). https://developer.android.com/guide/components/services

    2.) extend JobIntentService class, give it a try. And then override function onHandleWork(intent), where you start "heavy" work. https://developer.android.com/reference/androidx/core/app/JobIntentService

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