简体   繁体   English

当从 Android 的锁定屏幕单击通知操作时,如何以编程方式解锁屏幕?

[英]How can I unlock screen programmatically when the notification action is clicked from lock screen in Android?

When the screen is locked and the user chooses a positive response to a notification action from the lock screen, how do I add code to prompt a screen unlock?当屏幕被锁定并且用户从锁定屏幕选择对通知操作的积极响应时,如何添加代码以提示屏幕解锁? Here is the code to my current positive notification action that i have added to the notification builder, when this action is chosen I have added an intent in the class that extends the IntentService to navigate to an activity in the app: Here is the code to my current positive notification action that i have added to the notification builder, when this action is chosen I have added an intent in the class that extends the IntentService to navigate to an activity in the app:

private static Action yesLetsGo(Context context) {

        Intent intent = new Intent(context, NotificationIntentService.class);
        intent.setAction(NotificationTasks.ACTION_YES_LETS_GO_NOTIFICATION);

        PendingIntent yesLetsGo = PendingIntent.getService(
                context,
                ACTION_YES_LETS_GO_PENDING_INTENT_ID,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        Action yesLetsGoAction = new Action
                (R.drawable.ic_launcher_background,
                        "Yes Lets Go",
                        yesLetsGo);

        Log.i("Notification Utils","yesLetsGo method called");

        return yesLetsGoAction;

    }

For enabling or disabling lock screen in Android, we need to get the instance of KeyguardManager为了在 Android 中启用或禁用锁屏,我们需要获取KeyguardManager的实例

KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);  
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);  

and for disabling the lock use,和禁用锁的使用,

lock.disableKeyguard();

And of course we need the permission in the Manifest当然,我们需要 Manifest 中的许可

android.permission.DISABLE_KEYGUARD android.permission.DISABLE_KEYGUARD

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

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