Android activity over default lock screen
Asked Answered
C

4

77

How can I display an Activity or Dialog to be visible over the lock screen?

I have already tried displaying my lock activity when screen turns on by setting various window type in Activity.onCreate() method:

TYPE_PRIORITY_PHONE
TYPE_SYSTEM_ALERT
TYPE_KEYGUARD

and others together with SYSTEM_ALERT_WINDOW and INTERNAL_SYSTEM_WINDOW permissions.

My activity is visible after I unlock the device.

UPDATE:

I actually already managed to display my own Activity instead of default lock screen. It work perfectly unless you use HOME button.

Chart answered 2/9, 2010 at 16:33 Comment(8)
What exactly are you trying to accomplish? Are you trying to implement your own lock screen or you want your alert window to be shown over lock screen (whatever is is) or ... ?Almetaalmighty
Actually I wish I was able to implement both: own lock screen and a dialog over existing lock screen.Chart
I can tell you about showing a window over lock screen: see developer.android.com/reference/android/view/…Almetaalmighty
Thanks. However it is for API level 5 and higher. I am deleoping an app for API level 4.Chart
Would you be able to post that code?Lexical
@plugmind: I ran into same issue,can you plz share your code for reference ?Sacramental
is there any xml setting rather than in code?Weinberger
@plugmind, have you achieved the required task,,, I am stucking at the same....Paella
L
124
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|
            WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

try using this flags to disable lock screen when the activity is started.

After API level 17 you can use

<activity
        android:name=".yourActivityName"
        android:showOnLockScreen="true"
        android:screenOrientation="sensorPortrait" >

showOnLockScreen like in the example...

Latoyia answered 20/4, 2011 at 7:47 Comment(5)
this doesn't even require any permissions. Surprising.Reservist
not working in my case, is any special permission needed for this?Weinberger
The thing is that at least on 4.4 this does not work when there is a password set on the lock screen. does anyone know how to keep the lock screen on but still be able to display the dialogue over it?Nonrigid
Does anyone know how to deny home button in in case of custom activity for lock screen? Home button simply closes the activity and brings to home screen.Appleby
@karate There is Screen Pinning - developer.android.com/about/versions/android-5.0.html, but it does not seem to be very easy to use (the app must have Device Owner permissions)During
W
33

Don't go for activity, because android will not show lock screen behind your activity for security reason, so use service instead of Activity.

Below is my code in onStartCommand of my service.

WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

View mView = mInflater.inflate(R.layout.score, null);

WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 0, 0,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
    | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
    | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
/* | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON */,
PixelFormat.RGBA_8888);

mWindowManager.addView(mView, mLayoutParams);

And add <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> to manifest

Weinberger answered 31/12, 2014 at 8:32 Comment(6)
I could use this to show the layout on top of the display. But it is not receiving events, even if I add event handlers. How to overcome that problem?Borders
@Borders it will not receive events, because it's a compromise to be able to show your apps screen(which is not a system app) on lock screen. It's security issue. Generally Android didn't allow you to show your screen on lock screen having touch events,(e.g. you can steal some ones passwords by showing a transparent screen on lock screen)Weinberger
did you try using TYPE_SYSTEM_ERROR so that you can receive events?Sullen
The layout over lockscreen is not showing in full screen that is not covering the whole mobile screen.what to do for covering the whole mobile screenJugular
layout.score not found please help.Crazyweed
Can you explain what you mean by "android will not show lock screen behind your activity for security reason" given that other answers show how to use an activity and it works just fine? What is the benefit of this approach compared to the others?Veator
P
19

Use this in onCreate method

getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);  
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); 
// This line should be before setContentView..      
setContentView(......);

Hope this will work Thanks

Pyles answered 14/7, 2013 at 18:4 Comment(1)
@Subha, did you get any solution to avoid display while phone ringing. alarm ringing ?Jalap
E
0

You can use code in any of answers here that you think it's working. Then to prevent HOME button to work, change TYPE_SYSTEM_ALERT or TYPE_SYSTEM_OVERLAY (depends on what you currently use) to TYPE_SYSTEM_ERROR:

params = new WindowManager.LayoutParams(
    WindowManager.LayoutParams.MATCH_PARENT,
    WindowManager.LayoutParams.MATCH_PARENT,
    WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
    WindowManager.LayoutParams.FLAG_FULLSCREEN | 
    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | 
    WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD,
    PixelFormat.TRANSLUCENT
);
params.gravity = Gravity.TOP;
mOverlay = (RelativeLayout) inflater.inflate(R.layout.main, (ViewGroup) null);
mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
mWindowManager.addView(mOverlay, params);
Elinoreeliot answered 15/1, 2016 at 7:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.