简体   繁体   English

如何关闭Android设备?

[英]How to power off an Android device?

How could I power off an android device via java code? 我怎么能通过java代码关闭Android设备?
Is it even possible? 它甚至可能吗? Do I need special permissions to do this? 我需要特殊权限才能执行此操作吗?

There is no API in the Android SDK to allow user apps to do this. Android SDK中没有API允许用户应用执行此操作。

The closest you could perhaps try would be PowerManager.goToSleep() . 你可能尝试的最接近的是PowerManager.goToSleep()

Yes, It is possible but you need a Rooted device with Superuser Access. 是的,这是可能的,但您需要具有Superuser访问权限的Rooted设备。 Try using the following code: 尝试使用以下代码:

try {
    Process proc = Runtime.getRuntime()
                    .exec(new String[]{ "su", "-c", "reboot -p" });
    proc.waitFor();
} catch (Exception ex) {
    ex.printStackTrace();
}

If you plan on doing this without Root Privileges , forget it. 如果您计划在没有Root Privileges情况下执行此操作,请忘记它。 The only way to do that is to use PowerManager but that won't work unless your app is signed with the System Firmware Key . 唯一的方法是使用PowerManager但除非您的应用程序使用System Firmware Key签名,否则无法使用。

[via Programmatically switching off Android phone ] [通过编程关闭Android手机 ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM