Show dialog with touch events over lockscreen in Android 2.3
Asked Answered
M

3

7

I want to build a dialog which is visible on the lockscreen and can receive touch events. I built a window with WindowManager but only the TYPE_SYSTEM_OVERLAY Flag is shown over the lockscreen in GB (Android 2.3.7).

Is there a way to create a system overlay which is visible on the lockscreen and can receive touch events in Android 2.3.7?

There was a bug with FLAG_WATCH_OUTSIDE_TOUCH but I'm not sure how that affects me. Any ideas?

Margueritamarguerite answered 23/6, 2013 at 13:1 Comment(2)
You can see here http://androiddev.orkitra.com/?p=80753 It's worked for me.Japheth
This previous link is on NOD32's list of blocked sites.Merc
D
3

I do not think you can launch an activity each time when device is locked without binding your application as admin privilaged app programatically.

Once your app is admin privilaged app, you can programatically set password & lock the screen & then programatically unlock it using Device Policy Manager.

On top of that lock screen you can programatically launch your own activity & you can create your own unlocker & unlock device through that activity as you can get call backs via DeviceAdminReceiver.

Here is a good example for that & all you need is to create your own activity after you called DevicePolicyManager.lockNow(). Then it will appear on top of lock screen as normal activity plus extra control over native lockscreen.

Delaney answered 2/7, 2013 at 12:23 Comment(3)
Thank you for that. I tested it but it does not work. I added startActivity(new Intent(this, MainActivity.class)) after devicePolicyManager.lockNow() but the Activity is not visible over the lockscreen. Did I misunderstood you?Margueritamarguerite
Did you set the flag getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); in OnCreate of that activity. Otherwise it will not appear.Delaney
Okay you are right with this FLAG the activitiy is visible over the lockscreen. But if I want to set a height for the activity so it does not fill the whole screen it is not visible again. I tried with this code: layout.height = 200; window.setAttributes(layout); Is there a way to get this activity in front of the lockscreen?Margueritamarguerite
M
2

Try this It may helps you,

getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.alertdialog);

And also, Android is a little bit of a contradiction. It's very open and as a developer you have access to anything, and it's up to you to use those powers for good or evil. When I say evil I don't mean malware. I mean apps that try to get cute and use things in ways they weren't meant to be used, like putting up notifications asking you to use the app more. The contradiction is that you don't actually have access to everything, there are a few parts the developers decided were so important that app couldn't mess with them. The lock screen is one of those parts. You can replace your home app all you want, but you never have to worry about your replacement lock screen failing and preventing you from accessing your phone.

Even if this were possible you would have more problems to deal with. Every lock screen is different, manufacturers can and do customize it so you have no guarantees your activity won't get in the way of unlocking the phone.

For touching outside of your dialog,

dialog.setCanceledOnTouchOutside(your boolean);
Millwork answered 1/7, 2013 at 8:44 Comment(0)
T
1

Finally I achieved the same. Don't go for activity, because android will not show lock screen behind your activity for security reason, so for service.

Below is my code in onStartCommand of my service

WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

View mView = mInflater.inflate(R.layout.score, null);

WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams(
    ViewGroup.LayoutParams.WRAP_CONTENT,
    ViewGroup.LayoutParams.WRAP_CONTENT, 0, 0,
    WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
            | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
            | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
    /* | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON */,
    PixelFormat.RGBA_8888);

mWindowManager.addView(mView, mLayoutParams);
Takeo answered 21/5, 2014 at 10:40 Comment(2)
@patrick No, its not possible to get touch events for any window that is displayed on lock screen. If you add touch-events to it, then behind your window instead of lock-screen, launcher is shown. This is done for security purpose.Takeo
that's a pity, I needed something like that, fortunately I've managed to show an overlay when pressing a button in a widget :)Hustler

© 2022 - 2024 — McMap. All rights reserved.