Is there a way to disable the screensaver while my app is running?
The dimming of the screen is what I mean.
This question is related to
android
This can be done by acquiring a Wake Lock.
I didn't tested it myself, but here is a small tutorial on this.
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;
Its importante to note that these methods all must be run from the UI thread to work. See changing KeepScreenOn from javascript in Android cordova app
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);
}
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"
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.
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);
}
});
}
}
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
Simple add below line into your MainActivity
and your App never turn lights off.
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
this is the best way to solve this
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
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
Source: Stackoverflow.com