Android Lock Screen Widget
Asked Answered
H

2

80

A few users have been asking me Android lock screen widgets for my app - I believe they want a widget that stays on their lock screens and allows them to interact with the app.

I haven't been able to find any official documentation for this - the only thing I found was apps that will take home screen widgets and put them on the lock screen for you.

Any clues on where I learn more about building true lock-screen widgets?

Haland answered 7/11, 2010 at 0:57 Comment(7)
There is no official support for modifying the lock screen. In fact, I'd be rather surprised if it were possible to come up with a hack that worked across all devices.Faden
@Commonsware: Indeed there is. Check out the MixZing, they have an option to enable it, and that widget stays on top of lock screen. It's once available in free version and it works in my Desire, without rooting.Increase
@xandy: MixZing may have the option to be a lock-screen, but how is that supposed to help the questioner create one?Weigh
@MelindaGreen: With MixZing as example at-least we know that it is possibleKeen
Lock Screen Widgets have only been introduced with Android 4.2. So you might want to have another look at it.Emelina
^^My phone has android 4.1 and it has a lock screen widget for the default and the other music player,i even have a source code for the app which does soClosure
You may check my answer, I think will help you achieve what you want https://mcmap.net/q/83490/-creating-custom-lockscreen-in-android-closedDillydally
E
57

Lock screen interaction is difficult. Android allows basic operations with two window flags (FLAG_SHOW_WHEN_LOCKED and FLAG_DISMISS_KEYGUARD). FLAG_SHOW_WHEN_LOCKED works pretty consistently in that it will show on top of the lock screen even when security is enabled (the security isn't bypassed, you can't switch to another non-FLAG_SHOW_WHEN_LOCKED window).

If you're just doing something temporary, like while music is playing or similar, you'll probably mostly be okay. If you're trying to create a custom lock screen then there's a lot of unusual interactions on all the different android platforms. ("Help! I can't turn off my alarm without rebooting my HTC phone").

getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html

FLAG_SHOW_WHEN_LOCKED

Window flag: special flag to let windows be shown when the screen is locked.

FLAG_DISMISS_KEYGUARD

Window flag: when set the window will cause the keyguard to be dismissed, only if it is not a secure lock keyguard. Because such a keyguard is not needed for security, it will never re-appear if the user navigates to another window (in contrast to FLAG_SHOW_WHEN_LOCKED, which will only temporarily hide both secure and non-secure keyguards but ensure they reappear when the user moves to another UI that doesn't hide them). If the keyguard is currently active and is secure (requires an unlock pattern) than the user will still need to confirm it before seeing this window, unless FLAG_SHOW_WHEN_LOCKED has also been set. Constant Value: 4194304 (0x00400000)

Elmerelmina answered 12/1, 2011 at 6:57 Comment(8)
Thanks, but can I use this for widgets? From the documentation, which is sparse, this looks like something that can be used by an Activity view but I'm not sure how to set this for a widget, since the OS renders the widget and the app just sends over data for it.Haland
No, this can only be used for activities. The only way to display an actual Android "App Widget" ( developer.android.com/guide/topics/appwidgets/index.html ) on the lock screen would be through a program like (disclaimer, my app) WidgetLocker . MixZing's "Lock screen widget" is an activity, not an "App Widget".Elmerelmina
So how does MixZing make their activity look like a widget?Haland
You mean because of the system wallpaper? Theme_Wallpaper_NoTitleBar developer.android.com/reference/android/…Elmerelmina
What do you mean by "how does MixZing make their activity look like a widget"? I assumed it was because you saw the system wallpaper background but I guess not.Elmerelmina
Thanks @Kevin. This has been really helpful. One question though, what do you do to make the activity launch when the lockscreen is enabled, not just continue to show if the screen is locked while the activity is already active?Valeriavalerian
You can register a broadcast receiver for SCREEN_OFF and/or SCREEN_ON. Advantage of SCREEN_OFF is the user won't necessarily see the stock lock screen as it's already all set by the time the user turns on the screen. Advantage of SCREEN_ON is you can check inKeyguardRestrictedInputMode so you know that the stock lock screen is active. Sometimes the screen turns off for other reasons, like proximity sensor.Elmerelmina
I added those 2 lines of code into my Oncreate method, but it's not working for me. I have icons and texts such as Prev, Next, Play in the notification, but nothing showed up during the lock screen. Can you help me please? Thanks a lotZaidazailer
B
11

I had to implement a lock screen widget for my project. In the process, I accumulated a couple of resources.

  1. If you have an app that you want to put on the lock screen, first make it an appwidget. You can use the AppWidget class to do this.
  2. Now, use the AppWidgetHost class from the Android API to make your lock screen a host for the widgets. I don't know how to do this part, but there are some existing implementations like mylockandroid (links below).

Resources

http://code.google.com/p/mylockforandroid/ (NB This code is for older versions of Android. Android 4.2 and up has built in lockscreen widget support)

http://mylockandroid.blogspot.com/2010/03/widget-lockscreen-beta-11-r2.html

Blakeley answered 22/3, 2011 at 3:0 Comment(1)
Of course, if you want to display a widget on lock screen, you'll have to have an Activity created on SCREEN_ON, and within that Activity you can use AppWidgetHost. Because the way it stands in the answer, it is somewhat ambiguous.Restrictive

© 2022 - 2024 — McMap. All rights reserved.