以编程方式关闭 Android 手机
我们可以通过编程方式关闭 Android 手机吗?
我正在使用以下代码片段,但它对我不起作用。
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard(); // to disable
lock.reenableKeygaurd();// to enable
我也使用了该许可。
Can we switch off an Android phone programmatically?
I am using following snippet but it didn't work for me.
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard(); // to disable
lock.reenableKeygaurd();// to enable
and I used the permission also.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
正如 CommonsWare 已经说过的,这在普通 SDK 应用程序中是不可能的。您需要使用
系统固件密钥
对您的应用进行签名。但您的应用程序可以拥有Root
权限。尝试使用以下代码(如果您有 SU 访问权限):关机:
重新启动:
相同的代码,只需使用
“reboot”< /code> 而不是
“reboot -p”
。[另一方面:我在某处读到这些命令不适用于 Stock HTC ROM,但尚未证实]
As CommonsWare already said that this is not possible in an Ordinary SDK Application. You need to sign your app with the
System Firmware Key
. But it's possible for your app withRoot
privileges. Try using the following code (if you have SU access):Shutdown:
Restart:
Same code, just use
"reboot"
instead of"reboot -p"
.[On an other note: I read somewhere that these commands do not work on Stock HTC ROMs, but haven't confirmed myself]
您无法通过普通的 SDK 应用程序执行此操作。只有使用系统固件签名密钥签名的应用程序才能执行此操作。
You cannot do this from an ordinary SDK application. Only applications signed with the system firmware signing key can do this.
您可以使用 PowerManager 使其重新启动(这并不能保证它会重新启动 - 操作系统可能会取消它):
http://developer.android.com/reference/android/os/PowerManager.html#reboot(java.lang.String)
它需要 REBOOT 权限:
http://developer.android.com/reference/ android/Manifest.permission.html#REBOOT
您是否可以在尝试启用/禁用键盘保护时检查您的 logcat,并发布其中的内容?
You could possibly use the PowerManager to make it reboot (this does not guarantee that it'll reboot - OS may cancel it):
http://developer.android.com/reference/android/os/PowerManager.html#reboot(java.lang.String)
It requires the REBOOT permission:
http://developer.android.com/reference/android/Manifest.permission.html#REBOOT
Can you also check your logcat when trying to enable/disable keyguard, and post what's there?
更好的解决方案是运行:
su -c am start -a android.intent.action.ACTION_REQUEST_SHUTDOWN
您可以为此使用
Process
。A much better solution is to run:
su -c am start -a android.intent.action.ACTION_REQUEST_SHUTDOWN
You can use a
Process
for this.如果您拥有 sigOrSystem 权限 android.permission.SHUTDOWN,您可以像这样引发受保护的 ACTION_REQUEST_SHUTDOWN Intent:
请注意,在 Android 4.0 之前,此权限仅是签名权限。
If you have the sigOrSystem permission android.permission.SHUTDOWN you can raise the protected ACTION_REQUEST_SHUTDOWN Intent like this:
Note that prior to Android 4.0, this permission was signature only.
事实上,根据我的经验,上述回答并不完全准确。我正在尝试调暗屏幕的方法,发现以下内容:
如果我使用屏幕亮度值 0,而不是 0.005,实际上会关闭我的 Samsung Galaxy Tab。
我怀疑这是某个地方的错误,但我不这么认为有足够的硬件在其他 Android 型号上测试代码。因此,我无法真正告诉您手机上会发生什么。我可以告诉你,即使完全未签名,我的代码也会关闭我的手机。
Actually, the above responses are not completely accurate, in my experience. I was experimenting with ways to dim the screen and found that the following:
will actually turn my Samsung Galaxy Tab off if, instead of 0.005, I use a screen brightness value of 0.
I suspect this is bug somewhere, but I don't have sufficient hardware to test the code on other Android models. Hence, I can't really tell you what will happen on your phone. I can tell you that my code shuts of my phone even completely unsigned.