Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ask user to unlock device on clicking notification action in android?

Tags:

java

android


      I am displaying a notification from my application and this notification has an action in it, when user clicks on the action, the corresponding action class is called with the intent I set. Now, I want to perform a particular action but before that the user needs to unlock the screen if it is pin/pattern protected. I am not able to ask user to unlock device, i.e open up the unlock keypad/pattern on lock screen.
Below is the code I have,

    //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);

When user clicks on the notification action, I get the control to onHandleIntent in MyAction.java
In here, I want to request user to unlock device if password protected and then perform an action. How can I request user to unlock device in onHandleIntent?

I came across using KeyguardManager and KeyguardLock to acheive this but keyguardManager.newKeyguardLock is deprecated method and I want to avoid this. So, the next was using "FLAG_TURN_SCREEN_ON" and "FLAG_KEEP_SCREEN_ON" but I am unable to figure out how to use them in this context. I don't launch any window from my action class, it is just an operation like incrementing my counter. After clicking it the notification should disappear, perform my action and thats it.

I found a similar question Unlock phone , but the way it was did is by launching a dummy/empty activity.

Thanks in advance for any help, suggestions :)

like image 492
Destructor Avatar asked Jan 07 '16 14:01

Destructor


2 Answers

In the Activity you are opening (that is behind the lock screen) you should tell system to lift the keyguard (e.g. in onCreate())

For API >= 26

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

For API < 26:

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

If device is not locked securely it would unlock it immediately, and if it is locked, it will ask user to do it.

like image 187
tpaczesny Avatar answered Sep 21 '22 01:09

tpaczesny


Use Activity intent in pending intent insted of Service

Intent intent = new Intent(context, MyActivity.class);
intent.putExtra(key, "my_value"); //Used to send information to action class
PendingIntent pi = PendingIntent.getActivity(context, 0, intent,
                                 PendingIntent.FLAG_UPDATE_CURRENT);
like image 25
Sagar Kondhare Avatar answered Sep 20 '22 01:09

Sagar Kondhare