Xamarin Forms-在打开应用程序时禁用自动锁定

时间:2018-11-29 06:09:12

标签: xamarin xamarin.forms xamarin.ios xamarin.android

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

5 个答案:

答案 0 :(得分:1)

对于 iOS ,您需要覆盖Appdelegate类中的 @Configuration @EnableJpaRepositories(basePackages = "net.kzn.shoppingbackend") @EnableElasticsearchRepositories(basePackages = "net.kzn.shoppingbackend") @ComponentScan(basePackages = { "net.kzn.shoppingbackend" }) public class Config { @Bean public Client client() throws UnknownHostException { Settings settings = Settings.builder() .put("client.transport.sniff", true) .put("cluster.name", "elasticsearch").build(); @SuppressWarnings("resource") TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300)); return client; } @Bean public ElasticsearchOperations elasticsearchTemplate() throws UnknownHostException { return new ElasticsearchTemplate(client()); } } 方法:

DidFinishLaunchingWithOptions

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

您必须在AndroidManifest上声明此使用权限:

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

为WakeLock <uses-permission android:name="android.permission.WAKE_LOCK" /> 创建一个全局字段;

using static Android.OS.PowerManager;

在您的OnResume中:

private WakeLock wakeLock;

只需记住在执行以下操作暂停或销毁应用程序时释放此锁:

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

通常,建议在活动的 onResume()中调用 acquire 方法,并在 onPause中调用 release 方法()。这样,我们保证在暂停或恢复的情况下我们的应用程序仍然可以正常运行。

在查询的情况下恢复好运

答案 1 :(得分:1)

我们可以使用 Xamarin.Essentials 插件来实现。将其安装在解决方案级别(在安装时选择 include prerelease 复选框)。

在您的MainPage中编写此代码

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

在Android项目MainActivity中添加此行

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

在调用LoadApplication(new App());之前。有关更多信息,请访问Microsoft docs

这在整个应用程序中都有效。要安装插件,请参见下面的屏幕截图-

enter image description here

答案 2 :(得分:1)

Xamarin.Forms:

//show something important, do not sleep
DependencyService.Get<INativeTasks>().ExecuteTask("cannotSleep");

//can put in OnDisappearing event
DependencyService.Get<INativeTasks>().ExecuteTask("canSleep");

本机任务助手:

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

Android:

全局变量和其他..

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

本地助手:

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:

本地助手:

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 :(得分:0)

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

只需在任何页面中将DeviceDisplay.KeepScreenOn设置为true / false即可。无需进入单个平台。

不过,您必须在Android中设置权限WAKE_LOCK。

*主持人,我已删除另一篇文章的答案。这是与答案相关的文章。

答案 4 :(得分:0)

由于接受的答案已在android中弃用

以下代码在Xamarin.Android中对我有效

Window window = ((MainActivity)Forms.Context).Window;
window.AddFlags(WindowManagerFlags.KeepScreenOn);