start activity even when phone is locked

2019-08-22 01:53发布

问题:

I would like to start an activity like the one used when an alarm starts ringing and a dialog dismiss appears even when the phone is locked and I am able to click it. I have this code from this site but I don't know how to call it when I extend using broadcastreceiver. It always says

the method getWindow() is undefined type for...

I always seemed to get this error everytime I code, what does that mean.

public void unlockScreen() {
    Window window = getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
            + WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
            + WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
            + WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
}

回答1:

Register boradcast reciever for SCREEEN_ON intent. As screen gets on your braodcast reciever onRecieve(..) will execute, Start your actitvity with flags as you have written in your post in onRecieve method.