Android powermanager wakelock issue

368 Views Asked by At

I want to set the wakelock time to the "unlimited" time or at least set the time to xx minutes / hours. If I try these code :

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
wl.acquire();

It just give me 30 seconds for the wakelock. But if I change my code to wl.acquire(10*60*1000L /10 minutes/); as suggested from Android Studio, it didn't give any change to the wakelock time, any idea of it ?

1

There are 1 best solutions below

2
janithcooray On

Make sure you have the Manifest Permissions tag

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

make sure to call wakeLock.release() when leaving the activity

    @Override
    protected void onDestroy() {
        wakeLock.release();
        super.onDestroy();
    }

Wakelock doesn't exactly mean it will keep your screen on. if you want to keep the screen on :

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
                this.setTurnScreenOn(true);
            } else {
                final Window window = getWindow();
                window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
            }

then when you want to turn it off :

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
                this.setTurnScreenOn(false);
            } else {
                final Window window = getWindow();
                window.clearFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
            }