Skip to content

Instantly share code, notes, and snippets.

@dpolishuk
Created July 3, 2012 09:45
Show Gist options
  • Save dpolishuk/3038781 to your computer and use it in GitHub Desktop.
Save dpolishuk/3038781 to your computer and use it in GitHub Desktop.
acquire and release wakelock in the server
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