Skip to content Skip to sidebar Skip to footer

My App Crashes Just Because Wakelock "donotsleep"

My app works But when it goes to Stop service it just crashes because wakelock 'DoNotSleep' state so I will be glad to hear some advices. thats my logcat: 05-15 00:41:06.925: E/An

Solution 1:

You need to acquire the wake lock before you can release it:

http://developer.android.com/reference/android/os/PowerManager.WakeLock.html#acquire()

That's because wake locks are reference counted by default. If a wake lock is reference counted, then each call to acquire() must be balanced by an equal number of calls to release() (and vice versa). If a wake lock is reference counted and you release it more often than you acquire it (in your case you don't acquire it at all), then the WakeLock under-locked exception will be thrown.

http://developer.android.com/reference/android/os/PowerManager.WakeLock.html#setReferenceCounted(boolean)

Your code only creates a wake lock but it never acquires it. In order to balance the count, acquire it in onCreate():

publicvoidonCreate() {
    super.onCreate();

    // this only creates the wake lock without acquiring itPowerManagerpm= (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "DoNotSleep");

    // this acquires the wake lock
    wakeLock.acquire();
}

Solution 2:

Before you can release the Wakelock, you must first acquire it

publicvoidonCreate() {
    super.onCreate();
    PowerManagerpm= (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "DoNotSleep");

    // ADD THIS LINE
    wakeLock.acquire();
}

Post a Comment for "My App Crashes Just Because Wakelock "donotsleep""