Xamarin Forms - Disable auto-lock when the app is

2020-03-08 06:13发布

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

4条回答
混吃等死
2楼-- · 2020-03-08 06:54

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;
            }
        }
}
查看更多
对你真心纯属浪费
3楼-- · 2020-03-08 06:57

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

查看更多
Summer. ? 凉城
4楼-- · 2020-03-08 07:14

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 -

enter image description here

查看更多
我欲成王,谁敢阻挡
5楼-- · 2020-03-08 07:17

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.

查看更多
登录 后发表回答