How do I create an Activity that is visible on top of the lock screen

Russell Mitchell picture Russell Mitchell · Feb 15, 2011 · Viewed 7.9k times · Source

I'm receiving an incoming C2DM notification while the screen is locked. I'd like to wake up the screen and display the notification message on top of the lock screen using an Activity. I'm launching the notification Activity from my C2DM BroadcastReceiver as follows:

Intent new_intent= new Intent().setClass( context, EIAlertDialog.class );           
new_intent.addFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP );      
new_intent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );      
context.startActivity( new_intent );

and within the notification Activity's onCreate method, I wake up the screen as follows:

PowerManager powerManager= (PowerManager)getSystemService( Context.POWER_SERVICE );
if (!powerManager.isScreenOn()) {
    mWakeLock= powerManager.newWakeLock(
                   PowerManager.FULL_WAKE_LOCK |
                   PowerManager.ACQUIRE_CAUSES_WAKEUP, 
                   "My Tag" )
    mWakeLock.acquire();
}

The screen is woken up, but the notification Activity is not visible until I unlock the screen.

I realize that I can avoid the lock screen with the code below, but that is not desired. I want the user to unlock the phone, only if he/she is interested in reading/responding to the notification.

getWindow().addFlags(
    WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON 
);

Answer

num1 picture num1 · Feb 23, 2012

This is not possible as far as I know. It sounds like you are trying to replicate the iOS experiences. You should realize that android has its own conventions and violating them is not something you should do lightly.

Android is a little bit of a contradiction. It's very open and as a developer you have access to anything, and it's up to you to use those powers for good or evil. When I say evil I don't mean malware. I mean apps that try to get cute and use things in ways they weren't meant to be used, like putting up notifications asking you to use the app more. The contradiction is that you don't actually have access to everything, there are a few parts the developers decided were so important that app couldn't mess with them. The lock screen is one of those parts. You can replace your home app all you want, but you never have to worry about your replacement lock screen failing and preventing you from accessing your phone.

Even if this were possible you would have more problems to deal with. Every lock screen is different, manufacturers can and do customize it so you have no guarantees your activity won't get in the way of unlocking the phone.