How to prevent automatic screen lock on android by code?
Asked Answered
C

7

32

In my app there is a long loading process, and if the devices puts on the screen lock, my process stops for some reason.

How can i prevent the device from automatic screen lock?

Continuous answered 5/7, 2012 at 12:27 Comment(0)
M
27

you have to declare this uses-permission on AndroidManifest:

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

And in your code Activity:

PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Lock");
wakeLock.acquire();

Just remember to release this lock when your application is paused or destroyed by doing this:

wakeLock.release();

Usually, it's suggested to call the acquire method inside the onResume() of your activity and the release method in onPause(). This way we guarantee that our application still performs well in the case of being paused or resumed.

Maley answered 5/7, 2012 at 12:30 Comment(2)
FULL_WAKE_LOCK has been deprecated since API 17, FLAG_KEEP_SCREEN_ON is now the way to go :)Neckwear
PARTIAL_WAKE_LOCK may be more appropriate as it prevents the behaviour in the question (stopping the foreground app), but nothing else (like keeping display on).Lytle
H
47

Another way to keep the screen lock on without having to request the permission in the manifiest is:

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

Are you doing your long loading process in the UI thread? Something doesn't seem right - if the process is so long that the screen lock timesout and your process ends, perhaps it needs to go in a background service.

Herd answered 5/7, 2012 at 13:36 Comment(2)
How can I disable this flag again if I want ?Dismal
Try getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);Herd
M
27

you have to declare this uses-permission on AndroidManifest:

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

And in your code Activity:

PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Lock");
wakeLock.acquire();

Just remember to release this lock when your application is paused or destroyed by doing this:

wakeLock.release();

Usually, it's suggested to call the acquire method inside the onResume() of your activity and the release method in onPause(). This way we guarantee that our application still performs well in the case of being paused or resumed.

Maley answered 5/7, 2012 at 12:30 Comment(2)
FULL_WAKE_LOCK has been deprecated since API 17, FLAG_KEEP_SCREEN_ON is now the way to go :)Neckwear
PARTIAL_WAKE_LOCK may be more appropriate as it prevents the behaviour in the question (stopping the foreground app), but nothing else (like keeping display on).Lytle
R
8

XML Way:

Just use keepScreenOn attribute in your parent layout:

<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:keepScreenOn="true">

programmatically:

You can set it programmatically by adding flag:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
Radiocarbon answered 27/11, 2018 at 9:5 Comment(0)
A
5
KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();

in androidmanifest:

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

OR

Follow this link

Armagnac answered 5/7, 2012 at 12:31 Comment(0)
S
3

one interesting option that wasn't mentioned yet is the

View#setKeepScreenOn(boolean onOff)

method. This can actually be used dynamically at any point during the runtime of the app, e.g. for setting the screen mode depending on the app state

Schild answered 29/10, 2013 at 17:39 Comment(0)
S
3

The following line allows your phone to be ON when the app is in foreground

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

The following instructions allow us to disable the lock screen when lock button is pressed. KeyguardLock class was deprecated in API level 13

/*onCreate*/
KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE); // Deprecated :/
lock.disableKeyguard();

And in your manifest:

<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
Satsuma answered 28/8, 2015 at 10:2 Comment(0)
R
0

Go with the key guard code its working perfectly,

Just Paste the code in onCreate Method in your mainactivity file And Permission in android manifest file

Rupertruperta answered 16/7, 2015 at 12:17 Comment(1)
Support for another answer should be disclosed with an upvote, not by creating an answer.Gounod

© 2022 - 2024 — McMap. All rights reserved.