Srini Kandula
Srini Kandula

Reputation: 1001

android: which wake_lock should we be using

The android documentation says that I should be using

WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON

instead of

PowerManager.SCREEN_DIM_WAKE_LOCK. The link is here

But when I try to use it I get an exception saying it is an invalid lock level. I'm using API level 18, here is the code that fails.

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            PowerManager.WakeLock wl = pm.newWakeLock(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, "My Tag");

Has anybody tried this? Does it work?

Upvotes: 0

Views: 69

Answers (2)

Mohan
Mohan

Reputation: 311

The code below is working fine, check this

import android.os.PowerManager;

public class MyActivity extends Activity {

    protected PowerManager.WakeLock mWakeLock;

    @Override
    public void onCreate(final Bundle icicle) {
        setContentView(R.layout.main);

        /* This code together with the one in onDestroy() 
         * will make the screen be always on until this Activity gets destroyed. */
        final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        this.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
        this.mWakeLock.acquire();
    }

    @Override
    public void onDestroy() {
        this.mWakeLock.release();
        super.onDestroy();
    }
}

Don't forget to give permission in manifest file

<uses-permission android:name="android.permission.WAKE_LOCK" />

Upvotes: 0

Srini Kandula
Srini Kandula

Reputation: 1001

As per the API documentation one should be using the Window.addFlags() for keeping the screen on.

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

Upvotes: 2

Related Questions