how to unlock the screen when BroadcastReceiver is called?

后端 未结 2 424
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-15 00:31

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

相关标签:
2条回答
  • 2020-12-15 01:08

    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(....) :)

    0 讨论(0)
  • 2020-12-15 01:25

    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);
    
    0 讨论(0)
提交回复
热议问题