0

I get more than 20 errors with that msg in GoogleAnalytics:

RuntimeException (@android.os.PowerManager$WakeLock:release:***)

Android OS 2.3.*

Who can to explaing me - how to fix the problem & why app crashing?

My code:

private PowerManager.WakeLock wakeLock;

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    PowerManager pm = (PowerManager) activity.getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "chat");
}
@Override
public void onStart() {
    super.onStart();
    wakeLockToggle(true);
}

@Override
public void onStop() {
    super.onStop();
    wakeLockToggle(false);
}
    private void wakeLockToggle(boolean on){
    if(wakeLock == null) return;
    try {
        if(on && !wakeLock.isHeld()){
            wakeLock.acquire(200000);
        }

        if(!on && wakeLock.isHeld()) {
            wakeLock.release();
        }
    }catch(Throwable ignore){}

}

What's wrong i did?

Jaroslav Kadlec
  • 2,505
  • 4
  • 32
  • 43
Suleiman
  • 1,003
  • 2
  • 15
  • 29

1 Answers1

0

Using acquire with a timeout internally causes the WakeLock to acquire the lock and then post delayed to a handler a call to release() using your timeout value.

I suspect the error messages have to do with the WakeLock handler trying to release the WakeLock after the 200 second timeout while in the meantime your Fragment's onStop method has called and the lock released.

Michael Krause
  • 4,689
  • 1
  • 21
  • 25
  • because i want to keep screen on , but i don't want to keep screen on until user exit of app, therefore i use the WakeLock which automatically release after N*mills . – Suleiman May 19 '15 at 21:52
  • Is it another way to keep screen on within N*time and after user can close an application or activate screen manually – Suleiman May 19 '15 at 21:54
  • @IceJOKER: Please see my revised answer. I'm still not entirely sure why you would want to hold onto the WakeLock AFTER the user has navigated away from your app? – Michael Krause May 19 '15 at 22:46
  • I just want to keep screen on within(or while) N*seconds after user last active, and after if the user is not active OS can turn off screen for battery save. I don't want to keep screen on all time while user open app... just first N*seconds after start and after screen can to switch off. I hope i explain right – Suleiman May 20 '15 at 09:09
  • In that case, I think you should not ever call release yourself. Just keep track of the last time you called acquire with the timeout value and make sure not to call acquire again if the timeout value has not expired. E.g., if ((System.currTimeMillis() - lastAcquireTime) < TIMEOUT_VALUE_MILLIS) don't call acquire again. – Michael Krause May 20 '15 at 15:52
  • And no another way to do it? Why try catch black not catch ? – Suleiman May 20 '15 at 20:35