Home > Mobile >  Prevent screen from locking on Android
Prevent screen from locking on Android

Time:12-29

` android xamarin code

        lockctl = ((PowerManager)GetSystemService(Android.Content.Context.PowerService)).NewWakeLock(
           WakeLockFlags.Partial, "tag"); //cpu  on
        lockctl.Acquire();

        WifiManager wifi = ((WifiManager)GetSystemService(Android.Content.Context.WifiService));
        lockwifi = wifi.CreateWifiLock(Android.Net.WifiMode.Full, "wifilock");
        lockwifi.SetReferenceCounted(true);
        lockwifi.Acquire();

`

I am making a cctv app. The problem is that the screen turns off after a day. I want to keep this screen on.

plz help me

I want the screen to stay on even after several days

PLZ HELP ME MY APP SOLUTION

CodePudding user response:

Here is the code you can use to keep the screen awake and never goes off while your application is running.

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

This is one of the flag we can set to make the screen awake.

Put this inside your activity ex:- MainActivity inside onCreate().

CodePudding user response:

If your app is running foreground, you can keep the screen on by add the following code into the each activity's OnCreate method of your project:

        protected override void OnCreate(Bundle savedInstanceState)
        {
             ....
          this.Window.AddFlags(Android.Views.WindowManagerFlags.KeepScreenOn);
        }

Or you can just add the android:keepScreenOn="true" in the every layout.xml file, such as:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:keepScreenOn="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</RelativeLayout>

For more information, you can check the offical document about the keeping screen on.

  • Related