以编程方式关闭 Android 手机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3745523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically switching off Android phone
提问by Sri Sri
Can we switch off an Android phone programmatically?
我们可以以编程方式关闭 Android 手机吗?
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.
我也使用了许可。
采纳答案by xil3
You could possibly use the PowerManager to make it reboot (this does not guarantee that it'll reboot - OS may cancel it):
您可以使用 PowerManager 使其重新启动(这并不能保证它会重新启动 - 操作系统可能会取消它):
http://developer.android.com/reference/android/os/PowerManager.html#reboot(java.lang.String)
http://developer.android.com/reference/android/os/PowerManager.html#reboot(java.lang.String)
It requires the REBOOT permission:
它需要 REBOOT 权限:
http://developer.android.com/reference/android/Manifest.permission.html#REBOOT
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?
您还可以在尝试启用/禁用键盘保护时检查您的 logcat,并发布其中的内容吗?
回答by Sheharyar
As CommonsWare already saidthat 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 with Root
privileges. Try using the following code (if you have SU access):
正如CommonsWare 已经说过的,这在普通 SDK 应用程序中是不可能的。您需要使用System Firmware Key
. 但是您的应用程序可能具有Root
特权。尝试使用以下代码(如果您有 SU 访问权限):
Shutdown:
关掉:
try {
Process proc = Runtime.getRuntime()
.exec(new String[]{ "su", "-c", "reboot -p" });
proc.waitFor();
} catch (Exception ex) {
ex.printStackTrace();
}
Restart:
重新开始:
Same code, just use "reboot"
instead of "reboot -p"
.
相同的代码,只需使用"reboot"
而不是"reboot -p"
.
[On an other note: I read somewhere that these commands do not work on Stock HTC ROMs, but haven't confirmed myself]
[另外一个注意事项:我在某处读到这些命令在 Stock HTC ROM 上不起作用,但我自己还没有确认]
回答by CommonsWare
You cannot do this from an ordinary SDK application. Only applications signed with the system firmware signing key can do this.
您无法从普通 SDK 应用程序执行此操作。只有使用系统固件签名密钥签名的应用程序才能执行此操作。
回答by tristan2468
A much better solution is to run:
一个更好的解决方案是运行:
su -c am start -a android.intent.action.ACTION_REQUEST_SHUTDOWN
su -c am start -a android.intent.action.ACTION_REQUEST_SHUTDOWN
You can use a Process
for this.
您可以Process
为此使用 a 。
回答by Rupert Rawnsley
If you have the sigOrSystem permission android.permission.SHUTDOWN you can raise the protected ACTION_REQUEST_SHUTDOWN Intent like this:
如果你有 sigOrSystem 权限 android.permission.SHUTDOWN 你可以像这样提高受保护的 ACTION_REQUEST_SHUTDOWN Intent:
Intent intent = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
intent.putExtra("android.intent.extra.KEY_CONFIRM", false);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Note that prior to Android 4.0, this permission was signature only.
请注意,在 Android 4.0 之前,此权限仅为签名权限。
回答by Rich
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:
实际上,根据我的经验,上述回答并不完全准确。我正在尝试使屏幕变暗的方法,发现以下内容:
Window w = getWindow();
WindowManager.LayoutParams lp = w.getAttributes();
lp.screenBrightness =.005f;
w.setAttributes (lp);
will actually turn my Samsung Galaxy Tab off if, instead of 0.005, I use a screen brightness value of 0.
如果我使用屏幕亮度值 0 而不是 0.005,实际上会关闭我的三星 Galaxy Tab。
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.
我怀疑这是某个地方的错误,但我没有足够的硬件来测试其他 Android 型号上的代码。因此,我无法真正告诉您手机上会发生什么。我可以告诉你,即使完全没有签名,我的代码也会关闭我的手机。