Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 446
user2511882 Avatar asked Mar 18 '19 15:03

user2511882


People also ask

How do I enable shortcuts on lock screen?

Using the Keyboard: Press Ctrl, Alt and Del at the same time. Then, select Lock from the options that appear on the screen.

How can I tell if my home button is pressed Android?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken a text view.

How do I unlock my home screen without the power button?

Double-Tap to Lock and Unlock Android This feature can also be used to lock your phone—double-tap to lock, double-tap to unlock. Alternatively, if double-tapping your screen to lock and unlock your Android still isn't working, you can use a third-party app like Screen Off.


2 Answers

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

https://github.com/UdayaLakmal/LockScreenDemo

**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.

like image 101
UdayaLakmal Avatar answered Sep 28 '22 03:09

UdayaLakmal


            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" />
like image 35
user2511882 Avatar answered Sep 28 '22 05:09

user2511882