Search code examples
androidandroid-homebutton

Lock Screen detect home button


I recently downloaded the ACDisplay lock screen app:

https://play.google.com/store/apps/details?id=com.achep.acdisplay

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.


Solution

  •             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" />