10

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).

Community
  • 1
  • 1
Johannes Weiss
  • 52,533
  • 16
  • 102
  • 136

5 Answers5

3

I have been searching for an answer to the exact same question for a while. Apparently, after 2.0 onwards the device manager privileges for the app level were removed. But with Froyo - 2.2 the device policy manager is revealed granting us developers a multitude of administrative level controls.

http://developer.android.com/guide/topics/admin/device-admin.html

Orkun
  • 6,998
  • 8
  • 56
  • 103
1

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

sthg
  • 1,137
  • 3
  • 13
  • 32
  • 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 Weiss Apr 20 '09 at 18:24
  • Ah, might have misunderstood your question here. Is this about displaying the SCREEN itself or activating the system key lock? – sthg Apr 22 '09 at 00:30
  • 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 Weiss Apr 22 '09 at 17:21
1

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
  • 76,767
  • 18
  • 98
  • 146
  • 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 Weiss Apr 20 '09 at 18:27
  • 1
    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 Apr 20 '09 at 20:00
  • And in LockPatternKeyguardView there's a call to ScreenLock in the following fashion: View createLockScreen() { return new LockScreen( mContext, mLockPatternUtils, mUpdateMonitor, mKeyguardScreenCallback); } – JRL Apr 20 '09 at 20:01
  • 1
    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 Apr 20 '09 at 23:39
0

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 Otis
  • 31,257
  • 32
  • 136
  • 234
0

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
  • 10,071
  • 6
  • 34
  • 37