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 ?

peterh
  • 11,875
  • 18
  • 85
  • 108
Pachanka
  • 492
  • 2
  • 7
  • 19
  • 1
    I hope it is not, I wouldn't want my phone to unlock anytime! (I could dial by mistake...) – GôTô Sep 25 '10 at 10:47
  • 2
    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... – StrikeForceZero Mar 13 '13 at 18:54
  • 2
    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**. – Chris Stratton Dec 11 '13 at 16:55

4 Answers4

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*/
}
RenniePet
  • 11,420
  • 7
  • 80
  • 106
RN3KK Nick
  • 701
  • 6
  • 12
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.

hackbod
  • 90,665
  • 16
  • 140
  • 154
  • cool it works, but these flags only available on sdk version 5 and higher :( – mishkin Dec 12 '10 at 05:03
  • 2
    found universal approach, see my answer here http://stackoverflow.com/questions/3621599/wake-android-device-up/4541982#4541982 – mishkin Dec 27 '10 at 21:33
  • primarily can't get it working on ICS as a service. I even tried to create an activity that does this – Archimedes Trajano Feb 24 '12 at 22:24
  • 1
    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. – hackbod Mar 15 '12 at 16:32
  • 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. – Yar Apr 16 '12 at 18:18
  • @Yar did you end up getting this to work? I am having the same problem. – Forrest Bice May 21 '14 at 19:22
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(); 
Smern
  • 18,746
  • 21
  • 72
  • 90
Mitech
  • 400
  • 4
  • 6
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);
Carlos
  • 1,319
  • 2
  • 17
  • 20