Unlock the Screen Programmatically

允我心安 提交于 2019-11-27 23:55:06

问题


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


回答1:


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 than 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. 


来源:https://stackoverflow.com/questions/31066821/unlock-the-screen-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!