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上声明此use-permission:

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

通常,建议在活动的 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.

在查询的情况下恢复好运

Goodluck revert in case of queries