开发者

A background sensor data collector in Android

I am now programming a program that collects sensor data, e.g. acclerometer values for a whole day.

Current I just use an Activity and run the activity for a whole day (I turn off screen auto-black), and don't make any shortmessages or phone calls during the day.

I've heard I can make this kind of long running data collector in background using Service. But after I've checked the pe开发者_Python百科dometer at http://code.google.com/p/pedometer/. I found that, when the screen blacks out, the pedometer does not work. (But An application like predometer should work in any case as long as the power is on.)

Although I don't care about the power problem of always sensing acclerometers, I do want to black out the screen to save the power in screen to record more acclerometer data points.

I am thinking about two ways:

1.Using an Service, however, as the pedometer application showed. When the screen blacks out, the service seems stoped working too! Maybe the code has bugs.

2.My application is still an Activity. But I change the scrren light into 0 or totally black to save power.

My question is that: for 1) does a Service have the abality to be always running even when the screen blacks out for a long time; For 2, how to change the screen light?

Thanks!


concerning 1 - what you need is a remote service. this is a service nearly similar to a 'local' service (that is used in the pedometer example) but can run even if no activity is bound to it, in the background. you can turn off the screen and even the activity can crash (in a bad case ;) ) but the service keeps running if you started it with startService(...) instead of bindService(...).

try getting through this and see if that helps.

concerning 2 - you should really use (1) ;)


You do not need a remote service - this can be done with a local Service.

Use the command pattern instead of the binding pattern for the service i.e. use startService() / stopService() rather than bind() / unbind().

In your service onStartCommand() method, return Service.START_REDELIVER_INTENT or something similar, so that the service lives longer than the Activity.

Now the trick: to keep the service processing properly when the phone goes to sleep, you need to work with a PowerManager.WakeLock. These are quite difficult to get right, and I don't think a full explanation is needed in this answer.

Here is some more info:

  • How to get an Android WakeLock to work?

  • Also try Lars Vogel's blog: Using Android Wakelock – Staying up all night long


Apologies for the summary answer, but your question is quite broad in terms that it touches on some advanced topics.


Background service can be implemented with IntentService for the most of the scenarios.

Make sure that the background service works in latest and earlier Android version like 2.3 or 2.2.

Designing background operations is different starting from Android 4.0.

Best Practices for Background Jobs

Performing Network Operations

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜