android-How to run Service in different thread than main thread?

前端 未结 5 1843
旧时难觅i
旧时难觅i 2021-02-18 14:12

I am trying to develop a application in android that consists a service to read the sensor value for multiple hours. When i start the service my device get hang and all the othe

5条回答
  •  长情又很酷
    2021-02-18 14:51

    I think the answer to this problem lies in the power of IntentService. IntentService is a subclass of the regular Service class. There are a few key differences, however.

    Now, here's how you can take IntentServices to your advantage: IntentServices do not run on the main thread. Instead, they run on separate "Worker threads". This single attribute would solve your problem and eradicate the ANRs you are currently facing.

    To learn more about the differences between services and IntentServices, check this out.

提交回复
热议问题