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

后端 未结 5 645
走了就别回头了
走了就别回头了 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 17:51

    https://docs.microsoft.com/en-my/xamarin/essentials/device-display?tabs=android

    Just need to set DeviceDisplay.KeepScreenOn to true/false in any page and it will work. There is no need to go into individual platform.

    You'll have to set permission WAKE_LOCK in Android though.

    *Moderator, I have deleted my answer on another post. This is a more relevant post for answer.

    0 讨论(0)
  • 2020-12-18 18:02

    Since the accepted answer is deprecated in android

    Below code works for me in Xamarin.Android

    Window window = ((MainActivity)Forms.Context).Window;
    window.AddFlags(WindowManagerFlags.KeepScreenOn);
    
    0 讨论(0)
  • 2020-12-18 18:07

    We can achieve it by using Xamarin.Essentials plugin. Install it on solution level(While installing select include prerelease checkbox).

    In your MainPage write this code

    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            ToggleScreenLock();
        }
        public void ToggleScreenLock()
        {
            if (!ScreenLock.IsActive)
                ScreenLock.RequestActive();
            else
                ScreenLock.RequestRelease();
        }
    }
    

    In Android project MainActivity add this line

    Xamarin.Essentials.Platform.Init(this, savedInstanceState);
    

    before calling LoadApplication(new App());. For more information visit Microsoft docs.

    This is working throughout the app. To install plugin refer below screenshot -

    0 讨论(0)
  • 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:

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

    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

    0 讨论(0)
  • 2020-12-18 18:11

    Xamarin.Forms:

    //show something important, do not sleep
    DependencyService.Get<INativeTasks>().ExecuteTask("cannotSleep");
    
    //can put in OnDisappearing event
    DependencyService.Get<INativeTasks>().ExecuteTask("canSleep");
    

    Native tasks helper:

     public interface INativeTasks
        {
            ...
            void ExecuteTask(string task, object param=null);
            ...
        }
    

    Android:

    Global variables and other..

    public class DroidCore
    {
        private static DroidCore instance;
        public static DroidCore Current
        {
            get { return instance ?? (instance = new DroidCore()); }
        }
    
        public static Window MainWindow { get; set; }
        ...
    }
    

    MainActivity.cs

    protected override void OnCreate(Bundle bundle)
    {
    ...        
    DroidCore.Current.MainView = this.Window.DecorView;
    ...
    }
    

    Native helpers:

    public class NativeTasks : INativeTasks
        {
        public void ExecuteTask(string task, object param = null)
        {
                switch (task)
                {
    
                    ... //any native stuff you can imagine
    
                case "cannotSleep":
                    DroidCore.MainWindow.AddFlags(WindowManagerFlags.KeepScreenOn);
                    break;
    
                case "canSleep":
                    DroidCore.MainWindow.ClearFlags(WindowManagerFlags.KeepScreenOn);
                    break;
                }
            }
    }
    

    iOS:

    Native helpers:

    public class NativeTasks : INativeTasks
        {
        public void ExecuteTask(string task, object param = null)
        {
                switch (task)
                {
    
                    ... //any native stuff you can imagine
    
                case "cannotSleep":
                    UIApplication.SharedApplication.IdleTimerDisabled = true;
                    break;
    
                case "canSleep":
                    UIApplication.SharedApplication.IdleTimerDisabled = false;
                    break;
                }
            }
    }
    
    0 讨论(0)
提交回复
热议问题