Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
556 views
in Technique[技术] by (71.8m points)

android - Unlock the Screen Programmatically

I have a share button in the GCM notification. On click of the share button, I need to launch share intent. Everything works perfectly. Only problem that I'm facing is Lollipop lock screen feature. When I click share button from lock screen, my intent dialog appears below the lock screen and user has to unlock the screen to see the dialog. I want to unlock the screen programatically, when share button is clicked.

I tried with Power Manager, But all it's wakeClock flags are deprecated and WindowManager.LayoutParams.Flag_KEEP_SCREEN_ONis recommened to use. But I'm not using activity here. I'm using broadcastReciever context. and hence I cannot use getWindow()method.

I also tried with KeyguardManager. But even disableKeyguard() is deprectated.

I cannot use the Intent.ACTION_SCREEN_ON, as this should be used, if we want to perform any action after screen is unlocked.

i had used below intent to programmatically close the notification tray:

Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        mContext.sendBroadcast(it);

Is there a similar intent, that can be broadcasted to unlock the screen

Updated Code using DevicePolicyManager:

public static void handleShareBtnClick(Context context, String message) {
    GcmHelper helper = new GcmHelper();
    helper.shareMessage(context, message);
    if(Utility.isLollypopAndAbove()){
          helper.unlockLockScreen();
    }
    helper.launchShareforForAlert();

}



   public void unlockLockScreen(){
        DevicePolicyManager devicePolicyMngr= (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
        ComponentName compName=new ComponentName(mContext, DeviceAdminReceiver.class);
        if(!devicePolicyMngr.isAdminActive(compName))
            devicePolicyMngr.removeActiveAdmin(compName);
    }

Even after using DevicePolicyManager, It's not unlocking my screen

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Step 1: Add below code in your activity before setContentView(R.layout.example);

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|
            WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

Step 2: Lock your mobile, then you will see activity in which you have added this code. This will work even though your mobile is locked with pattern lock. This will work like a charm.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...