Xamarin Forms - Disable auto-lock when the app is open

后端 未结 5 663
走了就别回头了
走了就别回头了 2020-12-18 17:46

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

5条回答
  •  鱼传尺愫
    2020-12-18 18:08

    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;
           ....
        } 
    

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

    you have to declare this uses-permission on AndroidManifest:

    
    

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

    private WakeLock wakeLock;
    

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

    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

提交回复
热议问题