bivy
bivy

Reputation: 11

GPS LocationListener and phone sleeping

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 stuation?

Thanks in advance!

Upvotes: 1

Views: 1298

Answers (3)

afenkner
afenkner

Reputation: 486

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();

Upvotes: 1

magaio
magaio

Reputation: 647

How about a background Service that periodically gets launched using AlarmManager and goes back to sleep after persisting coordinates to a database or file?

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006674

Will phone go to sleep while such service is running?

Yes.

Upvotes: 0

Related Questions