0

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 ?

itsmebil
  • 155
  • 1
  • 9

1 Answers1

0

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);
            }
janithcooray
  • 188
  • 2
  • 15
  • I've tried these `@Override protected void onDestroy() { wakeLock.release(); super.onDestroy(); }` but nothing happened – itsmebil Nov 18 '20 at 07:30
  • then try this setTurnScreenOn(true) and window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON) – janithcooray Nov 18 '20 at 08:16