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.
Asked
Active
Viewed 8,451 times
2 Answers
8
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);

EboMike
- 76,846
- 14
- 164
- 167
-
Thanks, i will read the source code you provide here. Actually i've searched some codes before, but for some reasons, i cannot get the default AlarmClock source code. – kevin lynx Dec 04 '10 at 08:07
-
Yeah, because it's been deleted. The new hotness is "DeskClock". In any case, just take a quick peek at the KeyguardManager docs (I linked to it) and see if that's what you need. – EboMike Dec 04 '10 at 08:10
-
I glanced 'KeyguardManager' doc, and as it says :"Class that can be used to lock and unlock the keyboard.", i suppose that's what i need. But the api seems not very directyly. I need some time to try it. Thanks any way. – kevin lynx Dec 04 '10 at 08:26
-
2Ok, for my response will help others who will google here, i post my simple solution. As showed in the source code you gave to me, we can add some window flags to our Activity, and android will help us to do these things i'm asking here. Just add these codes to my notify Activity: 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); – kevin lynx Dec 04 '10 at 08:45
-
By the way - if you accept this answer, we'll both get rep points. Thanks! – EboMike Dec 04 '10 at 08:57
-
Actually i'm a newbie here, i clicked the icon at the right of your answer, i'm not sure whether i've accept your answer already. – kevin lynx Dec 04 '10 at 09:22
-
This seems far too easy and it does not work for me. I've tested it on 3 different devices (running 2.2, 2.3.3, and 2.3.4). They all turn the screen on but none unlocks. And the flags actually have an effect. I've tested this with FLAG_FULLSCREEN. Does anyone know if unlocking the screen/removing the keyguard still works??? – stfn Aug 12 '11 at 15:44
-
@steff: Are you using a password/pattern? Then it won't work, of course. See the docs: http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_DISMISS_KEYGUARD ... Also, is your activity a dialog or something? – EboMike Aug 12 '11 at 15:54
-
@EboMike: no password, no pattern, just a regular lockscreen. Of course I've been reading the docs. Regular Activity, nothing else. – stfn Aug 24 '11 at 13:13
-
I want to show my Activity from broadcastReceiver, I have reached till display on,,,, but my view is not showing over locked screen / idle state... I don't want to unlock, just want to add action like incoming call, like viber does.... I know this post is too old, but I have reached here from bar comment... http://stackoverflow.com/questions/8572449/bring-app-to-front-turn-on-display-and-unlock-from-alarmmanager – Abdul Wahab Jun 19 '14 at 18:50
0
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(....) :)

Junaid
- 3,477
- 1
- 24
- 24