简体   繁体   中英

In Android when collecting data using a sensor should I use IntentService or Service?

I'm making an app that will collect data using a sensor, only when the device is on. This program will mainly run from the notification bar. Most of the posts about using sensors in the background seem to suggest using Service but I've read that IntentServices are the way to go if handling a long task. So I was wondering which one would be best to use?

It sounds more like you would need a Service , as you will have a long-running operation (monitoring the sensors) which will pause (but not destroy) during some periods when the monitoring will not be required.

Using a Service will also allow your app's activities to bind to it, and show something like live updates. What you however need to be aware of is that if you would like to do something periodic in your Service then you will need to handle the scheduling and threading (if necessary, depending on the amount of processing required) yourself.

You could for example use an IntentService if you wanted to send the collected data to a remote server, as this will nicely kick off a worker thread for you to perform the (slow) network request on.

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