Running the service in the background forever ..? Android

I am doing a battery consumption study on an Android phone. I want to run Battery Check every 10 minutes until the battery dies completely. I had problems to make it work.

From my first attempt, I use the timer in the service class and plan to check the battery every 10 minutes. But I soon found that the service stopped when the screen went blank.

Then I try to use AlarmService, I use an alarm to wake up my service every 10 minutes and check the battery level and save the data in a file on the SD card. It works with a screen. However, I received the data in only 9 hours ... it seems that the AlarmService stops at some point after a few hours. I do not know why this is so, the system robbed him for memory problems?

So my question is: did anyone write any service to run (as always) in the background before? How do you do this and I really appreciate the sample code?

Currently, I am reading a few reports that there is a partial trace blocker that I can use to maintain the service. Is this the right way to do this?

Thanks a lot and I hope I can get some useful answers here.

+9
android
Feb 23 '10 at 23:44
source share
2 answers

During my first attempt, I use the timer in the service class and check the battery every 10 minutes. But soon I found that the service was suspended when the screen goes blank.

You probably did not save WakeLock , so the device died out.

AlarmService seems to stop after a few hours

I rather doubt it.

So my question is: did anyone write some service to run (as always) in the background before?

It is impossible to create a service that will work forever. It should be possible to create a scheduled task using the AlarmManager , which will be invoked forever.

I am currently reading several posts saying that there is a partial lock trail that I can use to keep the service alive .. is this the right way to do this?

I'm not sure what it is. But if you want the device to stay awake - whether for your first approach or just when doing the work caused by AlarmManager - you need to do a WakeLock .

Here is a project that almost exactly describes what you describe for AlarmManager , minus checking the battery level, but using the WakefulIntentService so that the device does not sleep. If you cannot get this code to work until the battery turns off, join the Google cw-android group and report your findings, and I'll take a look at it.

+10
Feb 24 2018-10-12T00
source share
+2
Feb 24 '10 at 11:41
source share



All Articles