Created
July 3, 2012 09:45
-
-
Save dpolishuk/3038781 to your computer and use it in GitHub Desktop.
acquire and release wakelock in the server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void acquireWakeLock() { | |
try { | |
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); | |
if (pm == null) { | |
Ln.e("LocationService: Power manager not found!"); | |
return; | |
} | |
if (wakeLock == null) { | |
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getPackageName()); | |
if (wakeLock == null) { | |
Ln.e("LocationService: Could not create wake lock (null)."); | |
return; | |
} | |
} | |
if (!wakeLock.isHeld()) { | |
wakeLock.acquire(); | |
if (!wakeLock.isHeld()) { | |
Ln.e("LocationService: Could not acquire wake lock."); | |
} | |
} | |
} catch (RuntimeException e) { | |
Ln.e("LocationService: Caught unexpected exception: " + e.getMessage(), e); | |
} | |
} | |
/** | |
* Releases the wake lock if it's currently held. | |
*/ | |
private void releaseWakeLock() { | |
if (wakeLock != null && wakeLock.isHeld()) { | |
wakeLock.release(); | |
wakeLock = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment