开发者

GPS LocationListener and phone sleeping

开发者 https://www.devze.com 2023-01-16 17:08 出处:网络
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 circu

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:

  1. Will phone go to sleep while such service is running?
  2. 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?

0

精彩评论

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

关注公众号