I've created service which has LocationListener in it. In order to keep service running the service is set as foreground. I have some questions about phone power management and sleeping in that circumstances:
- Will phone go to sleep while such service is running?
- How can I save power in this stuati开发者_如何学Goon?
Thanks in advance!
If you don't want the service to sleep then you can keep the device awake.
Snippet:
private PowerManager.WakeLock wakeLock; //member variable
somewhere in your service class:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "some_tag");
When you're done running then you can do:
wakeLock.release();
Will phone go to sleep while such service is running?
Yes.
How about a background Service that periodically gets launched using AlarmManager and goes back to sleep after persisting coordinates to a database or file?
精彩评论