Android disable screen timeout while app is running
AndroidAndroid Problem Overview
Is there a way to disable the screensaver while my app is running?
The dimming of the screen is what I mean.
Android Solutions
Solution 1 - Android
You want to use something like this:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Solution 2 - Android
I used:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
to disable the screen timeout and
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
to re-enable it.
Solution 3 - Android
There is also a XML way that Google recommends:
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:keepScreenOn="true">
Check Google Slides - Slide 16.
> "Wakelocks are costly if forgotten (...) Consider using > android:keepScreenOn to ensure correctness"
Solution 4 - Android
In a View, in my case a SurfaceView subclass, you can set the screen on always on. I wanted the screen to stay on while this view was still drawing stuff.
public class MyCoolSurfaceView extends SurfaceView {
@Override
protected void onAttachedToWindow (){
super.onAttachedToWindow();
this.setKeepScreenOn(true);
}
@Override
protected void onDetachedFromWindow(){
super.onDetachedFromWindow();
this.setKeepScreenOn(false);
}
Solution 5 - Android
Its importante to note that these methods all must be run from the UI thread to work. See https://stackoverflow.com/questions/18406722/changing-keepscreenon-from-javascript-in-android-cordova-app
Solution 6 - Android
Simple add below line into your MainActivity
and your App never turn lights off.
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Solution 7 - Android
Put this at onStart
> PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE); wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "no sleep"); wakeLock.acquire();
And this at you manifest
<uses-permission android:name="android.permission.WAKE_LOCK" />
Don't forget to
wakeLock.release();
at onStop
Solution 8 - Android
1.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);is best solution for Native Android.
2. if you want to do with React android application then please use the below code.
@ReactMethod
public void activate() {
final Activity activity = getCurrentActivity();
if (activity != null) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
});
}
}
Solution 9 - Android
this is the best way to solve this
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Solution 10 - Android
android:keepScreenOn="true"
is the better way to handle this if we want to make any particular screen awake all the time otherwise we have to handle yourself when to enable it and to disable it
https://developer.android.com/training/scheduling/wakelock.html
Solution 11 - Android
procedure SetSleep(aEnable:Boolean);
var
vFlags: integer;
begin
vFlags := TJWindowManager_LayoutParams.JavaClass.FLAG_TURN_SCREEN_ON or
TJWindowManager_LayoutParams.JavaClass.FLAG_DISMISS_KEYGUARD or
TJWindowManager_LayoutParams.JavaClass.FLAG_SHOW_WHEN_LOCKED or
TJWindowManager_LayoutParams.JavaClass.FLAG_KEEP_SCREEN_ON;
if aEnable then
begin
CallInUIThread ( // uses FMX.Helpers.Android
procedure
begin
TAndroidHelper.Activity.getWindow.setFlags (vFlags, vFlags);
end );
end
else
CallInUIThread (
procedure
begin
TAndroidHelper.Activity.getWindow.clearFlags (vFlags);
end );
end;
Solution 12 - Android
This can be done by acquiring a Wake Lock.
I didn't tested it myself, but here is a small tutorial on this.