views:

3993

answers:

7

I have to activate android's system key lock (the one you get when you press the power off/*hang up* button). See here:

img

I already browsed the docs but everything I found was PowerManager and KeyguardManager. Both seem not to be the solution :-(.

So, does everyone know how to achieve that from a android application? (If special permissions are required, that is no problem but changing the device's settings is not a solution...)

EDIT: Or does someone know that this is definitely not possible at all? Btw. craigs solution with sending keys does not work anymore (see comments).

(Target platform: Android 2.1, older versions would be cool but not necessary)

A: 

There's a pretty good example here:

http://www.anddev.org/throwing-simulating_keystrokes_programatically-t717.html

It looks like you can programmatically cause any keystroke to be sent to the system. It sounds like the keycode you're looking for is the KEYCODE_ENDCALL, but if that doesn't work there are plenty of other codes to try here:

http://developer.android.com/reference/android/view/KeyEvent.html

I don't know if there's any API call to cause the lock to occur, but this seems like a pretty sturdy workaround until you find a better solution.

craig
This method is no longer available, it was removed for security concerns.
Soonil
Ah bummer, are you aware of any other solution?
craig
Hmm, Android 1.0/the upcoming 1.5 are the target platform, so that won't work :-(
Johannes Weiß
A: 

Digging through the Android source found WindowManagerService which seems to have a public method (startAppFreezingScreenLocked) for activating this. This may be a good place to start looking for your answer, as unfortunately it doesn't seem as though you can directly get a WindowManagerService object.

Steve Pomeroy
+1  A: 

What you're looking for is the reenableKeyguard() method in KeyguardManager.KeyguardLock my friend !

sthg
Did you try that? Did the lock screen as you can see above really appear? I tried that and it did lock the keyboard but it was NOT the system keylock...
Johannes Weiß
Ah, might have misunderstood your question here. Is this about displaying the SCREEN itself or activating the system key lock?
sthg
sthg, both :-). I just want to activate the normal system keylock. The screen itself is part of that keylock. If the user activated a code(or a unlock-pattern), he should get asked about that code/pattern. Just locking the keyboard is not enough :-(
Johannes Weiß
A: 

Looks like the screen lock function is performed using the method:

public void goToSleep(long time)

method in PowerManager.java. It's possible to get a reference to it in this fashion:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);

However this requires the permission

android.permission.DEVICE_POWER

which is a level 2 permission available to the system only.

So looks like this isn't doable. This is for version 1.1 only, I don't know for 1.5.

JRL
hmm, that's bad. In fact, I think that that would not even be enough, because I really need the system key lock as you can see in the screenshot above. Nothing about that screen is mentioned in PowerManager's docs... It only says that the device will go to sleep but not that the system keylock gets activated.
Johannes Weiß
Not sure it'll help since it's version < 1.1 source but:goToSleep calls goToSleep in PowerManagerService then goToSleepLocked then setPowerState.And there's this comment in setPowerState:// When the user presses the power button, we need to always send out the// notification that it's going to sleep so the keyguard goes on. But// we can't do that until the screen fades out, so we don't show the keyguard// too early.
JRL
And in LockPatternKeyguardView there's a call to ScreenLock in the following fashion:View createLockScreen() { return new LockScreen( mContext, mLockPatternUtils, mUpdateMonitor, mKeyguardScreenCallback);}
JRL
Also, take at look at this thread. It seems to confirm this is not possible: http://groups.google.com/group/android-platform/browse_thread/thread/96dae5b512cb203b
JRL
P.S.: read the whole thread, it's quite informative.
JRL
A: 

I also have a need to lock the phone programatically -- as in, show the lock screen. Does anybody have any update on this?

amit
A: 

Is there any update?

David
no, I unfortunately don't think that anyone found a solution...
Johannes Weiß