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

前端 未结 5 1094
攒了一身酷
攒了一身酷 2021-02-18 14:29

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:35

    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.

提交回复
热议问题