Run a service in the background forever..? Android

前端 未结 2 644
北恋
北恋 2020-12-08 08:54

I am doing a Battery Consuming research on the Android phone. I want to run a Battery Check every 10 min till the battery totally dies. I have been having problems to make i

相关标签:
2条回答
  • 2020-12-08 09:27

    http://github.com/commonsguy/cw-advandroid/tree/master/SystemServices/Alarm/

    0 讨论(0)
  • 2020-12-08 09:33

    At my first try, I use a timer in a service class, and schedule the battery check every 10 mins. But soon I found that the service got paused when the screen goes off.

    You probably did not hold a WakeLock, so the device fell asleep.

    it seems that the AlarmService stop at some point after several hours

    I rather doubt it.

    So my question is, has anyone written some service to run (like forever) in the background before?

    It is not possible to create a service that will run forever. It should be possible to create a scheduled task via AlarmManager that will be invoked "forever".

    I am currently reading some posts saying that there's a partial wake lock I can use to keep the service alive.. is this the correct way to do it?

    I'm not sure what "it" is. But, if you want to keep the device awake -- whether for your first approach or just while you are doing work triggered by an AlarmManager -- you need to hold a WakeLock.

    Here is a project that does almost exactly what you describe for the AlarmManager, minus checking the battery level, but using a WakefulIntentService to ensure the device stays awake. If you cannot get this code to run until the battery shuts down, join the cw-android Google Group and report your findings, and I'll take a look at it.

    0 讨论(0)
提交回复
热议问题