Android - Activation of the system key lock (aka lock screen)

為{幸葍}努か 提交于 2019-11-29 13:19:50

问题


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

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


回答1:


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




回答2:


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




回答3:


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.




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/733721/android-activation-of-the-system-key-lock-aka-lock-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!