java - 如何要求用户在单击 android 中的通知操作时解锁设备?

标签 java android


我正在显示来 self 的应用程序的通知,并且该通知中有一个操作,当用户单击该操作时,将根据我设置的 Intent 调用相应的操作类。现在,我想执行一个特定的操作,但在此之前,如果屏幕受 pin/pattern 保护,用户需要解锁屏幕。我无法要求用户解锁设备,即在锁定屏幕上打开解锁键盘/图案。
下面是我的代码,

    //HandleAction is a java class that extends IntentService
    Intent intent = new Intent(context, HandleAction.class);
    intent.putExtra(key, "my_value"); //Used to send information to action class
    PendingIntent pi = PendingIntent.getService(context, 0, intent,
                                 PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder notification = new NotificationCompat.Builder(mContext);
    //set the title, icon etc for builder and add action as below
    notification.addAction(icon, "my_label", pi);

当用户点击通知 Action 时,我在 MyAction.java 中获取对 onHandleIntent 的控制
在这里,如果密码 protected ,我想请求用户解锁设备,然后执行操作。 如何在 onHandleIntent 中请求用户解锁设备?

我遇到过使用 KeyguardManager 和 KeyguardLock 来实现这一点,但 keyguardManager.newKeyguardLock 是不推荐使用的方法,我想避免这种情况。所以,下一个是使用“FLAG_TURN_SCREEN_ON”和“FLAG_KEEP_SCREEN_ON”,但我无法弄清楚如何在这种情况下使用它们。我没有从我的 Action 类中启动任何窗口,它只是一个像递增我的计数器这样的操作。单击它后,通知应该会消失,执行我的操作即可。

我发现了一个类似的问题Unlock phone ,但它的做法是启动虚拟/空 Activity 。

在此先感谢您的帮助和建议:)

最佳答案

在您打开的 Activity 中(在锁定屏幕后面)您应该告诉系统解除键盘锁定(例如在 onCreate() 中)

对于 API >= 26

KeyguardManager km = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
km.requestDismissKeyguard(this, null); // you may add callback listener here

对于 API < 26:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

如果设备未安全锁定,它会立即解锁,如果已锁定,它会要求用户解锁。

关于java - 如何要求用户在单击 android 中的通知操作时解锁设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34658292/

相关文章:

java - 如何在运行时切换 Spring 配置文件?

java - struts 2.3.16.1 升级为 Spring 框架中的 struts xwork 和 SimpleMessageListenerContainer 提供 TypeMismatchException

java - 带有 smartGWT : No palettes for smart widgets (XML UIBinder mode) 的 GWT 设计器

java - jSTL处理缺少url参数的情况

android - 是否可以跟踪不使用 Google Play 的应用引荐来源网址?

java - Facebook SDK 4.x Android 发布为 Facebook 页面

java - 如何将 2 个 Point3f 数组连接在一起?

android - 无法打开eclipse,启动屏幕闪烁但打不开

android - 部署在 android 上的 Hello world 应用程序在启动后意外停止

android - onDestroy 中 AsyncTask.execute 的含义