如何以编程方式关闭 Android 手机?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10411650/
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
How to shutdown an Android mobile programmatically?
提问by Venkat
Is it possible to shutdown the mobile programmatically. that is with out using su commands..
是否可以以编程方式关闭移动设备。那就是不使用 su 命令..
采纳答案by Krishnakant Dalal
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,并发布其中的内容吗?
You cannot do this from an ordinary SDK application. Only applications signed with the system firmware signing key can do this
您无法从普通 SDK 应用程序执行此操作。只有使用系统固件签名密钥签名的应用程序才能执行此操作
回答by Sheharyar
It is possible, but you need a Rooted
Android device with Superuser
access. You can't do it without Root unless your app is signed with the System Firmware Key
. Try using the following code:
这是可能的,但您需要Rooted
有Superuser
访问权限的Android 设备。除非您的应用程序使用System Firmware Key
. 尝试使用以下代码:
Shutdown:
关掉:
try {
Process proc = Runtime.getRuntime()
.exec(new String[]{ "su", "-c", "reboot -p" });
proc.waitFor();
} catch (Exception ex) {
ex.printStackTrace();
}
Reboot:
重启:
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 mk..
This is the code i use to perform any system command.
这是我用来执行任何系统命令的代码。
void shutdown_sys()
{
Process chperm;
try {
chperm=Runtime.getRuntime().exec("su");
DataOutputStream os =
new DataOutputStream(chperm.getOutputStream());
os.writeBytes("shutdown\n");
os.flush();
chperm.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Call this function from your android App. It will work if su is functional in your system. Let me know in case it does not work. I dont have an Android base ready to test. But the same works for reboot. So shutdown is also a linux shell command which ithink will be there in Android as well.. All the best
从您的 android 应用程序调用此函数。如果 su 在您的系统中正常运行,它将起作用。如果它不起作用,请告诉我。我没有准备测试的 Android 基础。但同样适用于重启。所以关闭也是一个 linux shell 命令,我认为它也将在 Android 中出现..一切顺利
回答by Ken Johnson
With rooted shell command type in, svc power shutdownworks for me in android 7.1
输入 root 的 shell 命令后,svc power shutdown在 android 7.1 中对我有用
回答by Chathuranga Chandrasekara
I don't see any resemblance with your title and the code snippet in the body. The snippet is related with KeyGuardManager which is used for lock and unlock the key pad.
我看不出与您的标题和正文中的代码段有任何相似之处。该代码段与用于锁定和解锁键盘的 KeyGuardManager 相关。
In order to shutdown your phone you will need to use PowerManager or SU commands. (On rooted devices.)
为了关闭您的手机,您需要使用 PowerManager 或 SU 命令。(在有根设备上。)
回答by Yogesh Rathi
If your device is rooted then you can try below solution
如果您的设备已植根,那么您可以尝试以下解决方案
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"
.