简体   繁体   English

使用部分唤醒锁关闭屏幕时阻止应用程序进入OnPause

[英]Prevent app from going on OnPause when turning screen off using partial wakelock

I'm trying to catch the volume Up/Down button press with the screen off, but despite acquiring a wakelock the app doesn't stay in the foreground when pressing the lock button. 我正在尝试在屏幕关闭的情况下按下音量上/下按钮,但是尽管获得了唤醒锁,但在按下锁定按钮时应用程序仍不会停留在前台。 I have a breakpoint in the OnPause method and it gets hit when the screen turns off, and I can confirm that the wakelock is on through ADB terminal with the command: 我在OnPause方法中有一个断点,当屏幕关闭时会被打断,我可以使用以下命令通过ADB终端确认唤醒锁已打开:

adb shell dumpsys power

I omitted the volume buttons events since they're not relevant, but they do work when the screen is on. 我忽略了音量按钮事件,因为它们不相关,但是在屏幕打开时它们确实起作用。 I don't know what I'm missing, maybe I'm missunderstanding how the wakelock is supposed to behave? 我不知道自己缺少什么,也许我误会了唤醒锁的行为方式?

I'm testing it in an emulator with Android 6.0 and in a physical phone with Android 7.1.2. 我正在Android 6.0模拟器和Android 7.1.2实体电话中对其进行测试。 Thanks in advance for any help.. 在此先感谢您的帮助。

Button getLockBtn;

PowerManager _powerManager;
PowerManager.WakeLock _wakeLock;


protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    RequestWindowFeature(WindowFeatures.NoTitle);
    SetContentView(Resource.Layout.Main);

    getLockBtn = FindViewById<Button>(Resource.Id.GetLockBtn);

    _powerManager = (PowerManager)GetSystemService(PowerService);
    _wakeLock = _powerManager.NewWakeLock(WakeLockFlags.Partial, "MyTag");

    getLockBtn.Click += (s, e) =>
    {
        if (_wakeLock.IsHeld)
        {
            _wakeLock.Release();
            getLockBtn.Text = "Get lock";
            //Remove the notification
        }
        else
        {
            //Here I show a notification
            _wakeLock.Acquire();
            getLockBtn.Text = "Release lock";
        }
    };
}

So how do apps like Google Maps achieve that behavior 那么像Google Maps这样的应用如何实现这种行为

When the screen is locked, the volume control comes from the system, and it is not the time to apply the control itself.Google Maps do nothing with the volume. 屏幕锁定后,音量控件来自系统,而不是时候应用控件本身.Google Maps对该音量不执行任何操作。

Your problem may not be the use of the volume button under the lock screen, but the program can still work when you lock the screen. 您的问题可能不是在锁定屏幕下使用音量按钮,但是在锁定屏幕时该程序仍然可以工作。

If you want a task to continue running, even when your Activity is not active, you need to use a Service. 如果您希望任务继续运行,即使您的活动处于非活动状态,则也需要使用服务。 https://developer.android.com/guide/components/services.html . https://developer.android.com/guide/components/services.html Also can and a Notification to show something on the lock screen. 还可以和一个通知在锁定屏幕上显示某些内容。 https://developer.android.com/guide/topics/ui/notifiers/notifications.html https://developer.android.com/guide/topics/ui/notifiers/notifications.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM