How to display Activity when the screen is locked?
Asked Answered
C

4

29

My application is launched on car docking event, I want to wake up phone (done by system) and unlock screen when I plug my device.

Is it posssible ?

Cassy answered 25/9, 2010 at 9:26 Comment(3)
I hope it is not, I wouldn't want my phone to unlock anytime! (I could dial by mistake...)Ariminum
I'm tempted to edit this to "How to display my activity when the screen is locked", as you can't "unlock" the device programatically in android...Guadalajara
Indeed, as SrikeForceZero explains, you cannot "unlock" the device programatically. What the answers below that seem to suggest you can are really doing is showing how you can make a specific Activity interactive while the device, overall, remains locked.Manrope
K
44

I'm use for upping activity to top level

    private Window wind;
    @Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    /******block is needed to raise the application if the lock is*********/
    wind = this.getWindow();
    wind.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
    wind.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    wind.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);
    /* ^^^^^^^block is needed to raise the application if the lock is*/
}
Knapsack answered 8/2, 2012 at 12:42 Comment(3)
Thanks. Works perfectly, even if I place it on OnCreate.Annamaeannamaria
I don't know. Please, test this method and tell us!Knapsack
Does not work on KitKat. Screen lights up but activity does not show until user unlock the screen.Chorion
L
21

Use Activity.getWindow() to get the window of your activity; use Window.addFlags() to add whichever of the following flags in WindowManager.LayoutParams that you desire: FLAG_DISMISS_KEYGUARD, FLAG_SHOW_WHEN_LOCKED, FLAG_TURN_SCREEN_ON

This is how the standard car dock (and desk dock) app implements this behavior.

Lolland answered 25/9, 2010 at 17:47 Comment(6)
cool it works, but these flags only available on sdk version 5 and higher :(Rappee
found universal approach, see my answer here https://mcmap.net/q/369604/-wake-android-device-up/…Rappee
primarily can't get it working on ICS as a service. I even tried to create an activity that does thisActing
At this point API 5 and higher has about 99% of the market. I see no reason to use tricks when you can just use these flags.Lolland
Somehow for me this does not work. I added the flags in onCreate() of my activity, which is called by my BroadcastReceiver, upon an alarm incoming, set by AlarmManager. The onCreate is called, the flags are set but the phone remains black and locked.Ladoga
@Ladoga did you end up getting this to work? I am having the same problem.Quickwitted
C
4

You will be able to use FLAG_DISMISS_KEYGUARD only for phones which do not have security enabled locks like pattern lock.

FLAG_SHOW_WHEN_LOCKED will only bring your current Activity on the top, if user tries to move elsewhere, he will have to unlock the screen.

Alternatively, you can add permission in your manifest:

<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>

And, in your activity on create:

KeyguardManager manager = (KeyguardManager) this.getSystemService(Context.KEYGUARD_SERVICE);
KeyguardLock lock = manager.newKeyguardLock("abc");
lock.disableKeyguard(); 
Chassis answered 26/8, 2013 at 19:53 Comment(0)
D
4

When using a lock pattern or a pin entry I needed to add the following as well because the screen turned off in less than 5 seconds:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Debussy answered 23/2, 2014 at 5:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.