在运行通知操作之前,请解锁设备

问题描述 投票:0回答:2

带两个按钮(动作)的通知。我处理的是:

    public class NotificationActionHandler : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
}

目标:用户在允许按下按钮之前解锁设备。我尝试了一些东西,但它会允许按下按钮

我怎么解决这个问题?我试过这个:How to ask user to unlock device on clicking notification action in android?

xamarin.android
2个回答
1
投票

同意hakim,您能否提供有关此问题的更多信息?如果您不知道如何为requestDismissKeyguard函数进行回调。您可以像下面的代码一样创建一个新的KeyguardDismissCallback

 km.RequestDismissKeyguard(this, new MyKeyguardDismissCallback());


 public class MyKeyguardDismissCallback : KeyguardDismissCallback
    {
        public override void OnDismissError()
        {
            base.OnDismissError();
        }

        public override void OnDismissSucceeded()
        {
            base.OnDismissSucceeded();
        }

        public override void OnDismissCancelled()
        {
            base.OnDismissCancelled();
        }
    }

如果您不知道如何在广播接收器中获取活动。您可以使用此Xamarin.Android插件来获取正在显示的当前活动。

var activity = Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity;


1
投票

我无法正确理解你的问题,因为它有点模棱两可,所以,我只是假设你无法转换代码并为你做这件事:

对于API> = 26

KeyguardManager km = (KeyguardManager)GetSystemService(Context.KeyguardService);
km.RequestDismissKeyguard(this, null); // you may add callback listener here

对于API <26

Window.AddFlags(WindowManagerFlags.DismissKeyguard);

或者在Notification服务中的Notification pending for intent中使用Activity for intent in pending intent而不是Service:

 Intent intent = new Intent(Context, typeof(YourActivity));
 intent.PutExtra(key, "my_value"); //Used to send information to action class
 PendingIntent pi = PendingIntent.GetActivity(Context, 0, intent,
                                         PendingIntentFlags.UpdateCurrent);
© www.soinside.com 2019 - 2024. All rights reserved.