tags:

views:

1165

answers:

2

My WakeLock isn't keeping my device awake.

In OnCreate() I've got:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "My Tag");
mWakeLock.acquire();

then:

new CountDownTimer(1320000, 200) {

    public void onTick(long millisUntilFinished) {
        // I update a progress bar here.                                         
    }

    public void onFinish() {
        // I finish updating the progress bar.
        mWakeLock.release();
    }
}.start();

The screen turns off before the timer finishes, how can I make the screen stay visible?

mWakeLock is a field previously declared like so:

private PowerManager.WakeLock mWakeLock;

My device uses Android 1.6. I would really appreciate any help to resolve this.

+3  A: 

Do you have the required permission set in your Manifest?

<uses-permission android:name="android.permission.WAKE_LOCK" />
wf
Indeed. Check your logcat output at the time you call the wakelock. The system usually warns you of missing permissions.
Christopher
I do have those permissions set, and the Android OS mentions the permissions when I install the app.
Curyous
A: 

I am having a similar problem. I can get the screen to stay on, but if I use a partial wake lock and the screen is turned off, my onFinish function isn't called until the screen is turned on.

You can check your wake lock using mWakeLock.isHeld(), first of all, to make sure you're getting it. Easiest way is to add that to the code, set a breakpoint on it in the debugger, and then check it.

In my case, I'm getting it, but the partial wake lock doesn't seem to be doing anything. Here's my working code for the screen dim lock.

protected void setScreenLock(boolean on){
        if(mWakeLock == null){
            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK |
                                        PowerManager.ON_AFTER_RELEASE, TAG);
        }
        if(on){
         mWakeLock.acquire();
        }else{
            if(mWakeLock.isHeld()){
                mWakeLock.release();
            }
         mWakeLock = null;
        }

    }

ADDENDUM:

Droid Eris and DROID users are reporting to me that this DOES NOT work on their devices, though it works fine on my G1. What device are you testing on? I think this may be an Android bug.

Brock Tice