Lock Screen detect home button
Asked Answered
S

3

15

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.

Sharitasharity answered 18/3, 2019 at 15:5 Comment(0)
S
1
            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" />
Sharitasharity answered 16/5, 2019 at 15:29 Comment(0)
S
4

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://mcmap.net/q/539989/-how-can-i-detect-user-pressing-home-key-in-my-activity-duplicate 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.

Shurwood answered 26/3, 2019 at 9:43 Comment(6)
Can you provide a sample app which demonstrates how to display an image as a lock screen from example when the device is locked and unlocked by the user?Sharitasharity
@Sharitasharity update answer with sample project. in lockscreen it hide menu button and also can get home button press event, check logs.Shurwood
the stackoverflow link that you pointed to clearly says that the approach has been deprecated from Android L and above.In your sample, when i hit the lock button, the device does not go to sleep since there is an intent. What i am trying to replicate, is the same behavior as ACDisplay where if the screen is turned back on, the lock screen is already visible. Please note, that it IS ALREADY visible. To paraphrase, if i hit the power button, the lock screen should takes it place without waking the device and when i wake up the device, the lock screen should already be in place.Sharitasharity
@Sharitasharity as you asked " sample app how to display an image as a lock screen from example when the device is locked and unlocked by the user", demo only does that launch activity when device lock and again when unlock. as i mention in edit that get running task not required since this only use when device lockShurwood
if you need only get screen on/off event you can register github.com/UdayaLakmal/LockScreenDemo/blob/master/app/src/main/… filter.addAction(Intent.ACTION_SCREEN_ON); too and launch activity when broadcast event receive, And running that forever you have to do that with service.Shurwood
can you share how are you detecting the home AND recent button and stop the user from exiting the app on your playstore application? Are you throwing an intent?Sharitasharity
S
1
            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" />
Sharitasharity answered 16/5, 2019 at 15:29 Comment(0)
C
0

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.

Casing answered 18/3, 2019 at 15:21 Comment(2)
Then how is the application able to do it?Sharitasharity
I used it, but I don't know how it works internally.Casing

© 2022 - 2024 — McMap. All rights reserved.