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.

user2511882
  • 9,022
  • 10
  • 51
  • 59

3 Answers3

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://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.

UdayaLakmal
  • 4,035
  • 4
  • 29
  • 40
  • 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? – user2511882 Mar 26 '19 at 14:20
  • @user2511882 update answer with sample project. in lockscreen it hide menu button and also can get home button press event, check logs. – UdayaLakmal Mar 28 '19 at 06:16
  • 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. – user2511882 Mar 29 '19 at 16:04
  • @user2511882 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 lock – UdayaLakmal Mar 30 '19 at 09:25
  • if you need only get screen on/off event you can register https://github.com/UdayaLakmal/LockScreenDemo/blob/master/app/src/main/java/com/udayalakmal/androidlockscreendemo/MainActivity.java#L56 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. – UdayaLakmal Mar 30 '19 at 09:26
  • 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? – user2511882 May 13 '19 at 22:55
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" />
user2511882
  • 9,022
  • 10
  • 51
  • 59
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.

iamkdblue
  • 3,448
  • 2
  • 25
  • 43