Lock Screen detect home button

Asked by At

I recently downloaded the ACDisplay lock screen app:


The application shows an overlay on my device while also at the same time detecting home button click on the activity so that i can't bypass the lock screen. It is also somehow able to completely hide the recent button on a non-rooted device. How is that possible?

I have went through the following links:

Detect home button press in android

How can I detect user pressing HOME key in my activity?

How can I detect user pressing HOME key in my activity?

and all of the solutions used to work for older versions of Android OR they say that the home button click cannot be detected since it can be used by malicious apps.

How is this application able to do so?

Can someone share sample code/app on how to prevent the user exiting the lock screen app until they have successfully authenticated themselves?

Thank you.

3 Answers

user2511882 On Best Solutions
            WindowManager windowManager  = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
            WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
            } else {
                layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
            layoutParams.gravity = Gravity.TOP | Gravity.LEFT;
            layoutParams.x = 0;
            layoutParams.y = 0;
            layoutParams.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                    | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
            View window = LayoutInflater.from(this).inflate(R.layout.activity_main, null, false);

            windowManager.addView(window, layoutParams);

The following piece of code blocks the home, back and recents button as per the requirement.

Also requires the following permission in the manifest:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
kdblue On

You cant disable recent button and home button but you can achieve this by using Window Manager link, in one line its create an overlay over your android application screen.

UdayaLakmal On

With Device admin permission https://developer.android.com/guide/topics/admin/device-admin you can pragmatically lock unlock device.

That app also use permission for "retrieve running apps" android.permission.GET_TASKS, so with that we can detect current foreground running app. check answer. https://stackoverflow.com/a/17756786/1025070 with that if user try to press home and leave we can instant check app is not in forground, and relaunch our activity again. (its workaround to detect if user leave from app with Home press).

Check my app https://play.google.com/store/apps/details?id=com.udayalakmal.applock&hl=en that add overlay of lockscreen on any app that can not bypass. use same check foreground running app.

@user2511882 - Created sample app that simply load Activity when device locked, and another activity when device unlock


**This is only a demo, you have to use receivers with background service for continue monitor device lock state and handle memory leaks, .Tested with Android 6 API 23 no need to monitor running apps since this only use with device lock screen.

**Check how I get Home button press event in Lockscreen activity.