xamarin 防止屏幕锁定在Android上

qni6mghb  于 2022-12-31  发布在  Android
关注(0)|答案(2)|浏览(203)

安卓xamarin代码

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();

'
我正在做一个闭路电视应用程序,问题是一天后屏幕就会关闭。
我想让这个屏幕一直亮着。我想让这个屏幕即使在几天后也一直亮着

eufgjt7s

eufgjt7s1#

以下代码可用于在应用程序运行时保持屏幕处于唤醒状态,并且永不熄灭。

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

这是我们可以设置的使屏幕唤醒的标志之一。
将其放入您的活动中,例如:-onCreate()中的MainActivity。

yqlxgs2m

yqlxgs2m2#

如果您的应用在前台运行,您可以通过将以下代码添加到项目的每个Activity的OnCreate方法来保持屏幕打开:

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

或者您可以只在每个layout.xml文件中添加android:keepScreenOn="true",例如:

<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>

要了解更多信息,您可以查看有关保持屏幕打开的官方文件。

相关问题