Xamarin Forms - 在应用程序打开时禁用自动锁定 [英] Xamarin Forms - Disable auto-lock when the app is open

查看:25
本文介绍了Xamarin Forms - 在应用程序打开时禁用自动锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序打开时禁用自动锁定.我怎样才能做到这一点?

I want to disable auto-lock when my app is open. How can I do that?

推荐答案

对于 iOS,您需要覆盖 Appdelegate 类中的 DidFinishLaunchingWithOptions 方法:

For iOS, you need to override the DidFinishLaunchingWithOptions method in your Appdelegate class:

 public override bool FinishedLaunching(UIApplication uiApplication, NSDictionary launchOptions)
    {
       UIApplication.SharedApplication.IdleTimerDisabled = true;
       ....
    } 

对于 Android,您需要在 MainActivity 中为其执行以下操作:

For Android, you need to do the following things in your MainActivity for it:

你必须在 AndroidManifest 上声明这个使用权限:

you have to declare this uses-permission on AndroidManifest:

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

为 WakeLock 创建一个全局字段 using static Android.OS.PowerManager;;

Create a global field for WakeLock using static Android.OS.PowerManager;;

private WakeLock wakeLock;

在你的 OnResume 中:

And in your OnResume:

PowerManager powerManager = (PowerManager)this.GetSystemService(Context.PowerService);
WakeLock wakeLock = powerManager.NewWakeLock(WakeLockFlags.Full, "My Lock");
wakeLock.Acquire();

只要记住在你的应用程序被暂停或销毁时释放这个锁:

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

wakeLock.Release();

通常,建议在您的 Activity 的 onResume() 中调用 acquire 方法,在 onPause 中调用 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.

如有查询,幸运回复

这篇关于Xamarin Forms - 在应用程序打开时禁用自动锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆