简体   繁体   English

如何以编程方式关闭Android 4.0 rooted Device

[英]How to Shutdown Android 4.0 rooted Device programatically

Please help me to find out solution for this, I Know there are so many questions and duplicates about this same but here i describe whole things which i tried. 请帮助我找到解决方案,我知道有很多问题和重复这个相同,但在这里我描述了我尝试过的所有事情。

I have one android device where its installed 4.0 version of android. 我有一个Android设备,其中安装了4.0版本的android。 I want to shutdown this device using my one demo application. 我想使用我的一个演示应用程序关闭此设备。

1) Demo application is signed by platform keys which are used in built in file system.
2) Device is already rooted as its development board and i have all permissions on this.

Application contains Following things 应用包含以下内容

1) Application is system application
2) Application signed by platform keys which are used in built in file system.

For make automation easier, I did import the key/cert pair into my java keystore file, with the this keytool-importkeypair and use eclipse for signing. 为了简化 make自动化,我确实将key/cert对导入到我的java keystore文件中,使用此keytool-importkeypair并使用eclipse进行签名。

Used command is mentioned below. 使用的命令如下所述。

Commad : keytool-importkeypair -k ~/Desktop/myown.keystore -p android -pk8 platform.pk8 -cert platform.x509.pem -alias platform Commad: keytool-importkeypair -k ~/Desktop/myown.keystore -p android -pk8 platform.pk8 -cert platform.x509.pem -alias platform

I used following code for reboot but i never make success in this .I read So many questions and answers on stackoverflow but they all said you require 我使用以下代码进行重启,但我从未在这方面取得成功。我在stackoverflow上阅读了很多问题和答案,但他们都说你需要

1) root access of device
2) signed apk with any one keys which are available on `build/target/product/security/`
3) Given Proper permission in AndroidManifest.xml file.

Am i right in alomg points? 我在alomg点吗?

Application code : 申请代码:

First Try 第一次尝试

public static void shutdown2() {

Runtime runtime = Runtime.getRuntime();
Process proc = null;
OutputStreamWriter osw = null;

String command = "/system/bin/reboot -p";

try { // Run Script

    proc = runtime.exec("/system/xbin/su");
    osw = new OutputStreamWriter(proc.getOutputStream());
    osw.write(command);
    osw.flush();
    osw.close();

} catch (IOException ex) {
    ex.printStackTrace();
} finally {
    if (osw != null) {
        try {
            osw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
try {
    if (proc != null)
        proc.waitFor();
} catch (InterruptedException e) {
    e.printStackTrace();
}
if (proc.exitValue() != 0) {
}
 }

Second Try : 第二次尝试:

private void shutdown3() {
        try {

            String[] cmd = { "/system/bin/sh","su","reboot -p"};

            Process proc = Runtime.getRuntime().exec(cmd);
            proc.waitFor();
        } catch (Exception ex) {
            Log.i("TAG", "Could not reboot 3  ", ex);
        }
    }

3rd Try : 第三次尝试:

private void shutdown() {
        try {
            Process proc = Runtime.getRuntime().exec(
                    new String[] { "/system/bin/su", "-c",
                            "/system/bin/reboot -p" });
            proc.waitFor();
        } catch (Exception ex) {
            Log.i("TAG", "Could not reboot 1 ", ex);
        }
    }

In 3rd method I also tried with "/system/bin/su" 在第三种方法中我也试过"/system/bin/su"

The below code worked for me 以下代码对我有用

        Process process = Runtime.getRuntime()
                .exec(new String[]{ "su", "-c", "busybox poweroff -f"});
        process.waitFor();

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. 您可以使用Process来实现此目的。

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

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