how to unlock the screen when BroadcastReceiver is called?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-18 01:13:06

问题


As you can guess, i register an alarm by AlarmManager. And the BroadcastReceiver will be called correctly. But when it called, my phone screen is still locked. I notice the default AlarmClock application is not like this. So my question is, how to unlock the screen when the BroadcastReceiver is called ? (Unlock the screen can make the user to operate my Activity directly) Thanks in advance.


回答1:


The source code for the alarm clock is in the Android source code. AlarmClock is gone, but has been replaced by DeskClock. Source code is here. I glanced over the code real quick, and their receiver seems to use the KeyguardManager. Check out the docs, that seems to be what you want.

EDIT: I'll add your findings here. This code should do:

final Window win = getWindow();
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
              | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); 
win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
              | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);



回答2:


Open the Activity A which you want to start from onReceive(....). Paste this in onCreate() of that Activity A

     final Window win= getWindow();
    win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | 
              WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
               WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

Make sure you are not pasting it before setContentView(....) :)



来源:https://stackoverflow.com/questions/4352548/how-to-unlock-the-screen-when-broadcastreceiver-is-called

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