2013-04-18 48 views
3

请帮我找出解决方案,我知道有这么多的问题和重复有关这一点,但在这里我描述了我试过的整个事情。如何以编程方式关闭Android 4.0植根设备

我有一个Android设备,其中安装了4.0的Android版本。 我想使用我的一个演示应用程序关闭此设备。

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. 

应用包含以下几点

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

对于化妆的自动化容易,我做进口key/cert对进入我的Java keystore文件,与此keytool-importkeypair和使用Eclipse进行签名。

使用的命令如下所述。

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

我用下面的代码重新启动,但我从来没有在这。我的成功读计算器这么多的问题以及答案,但他们都表示你需要

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. 

上午我在阿隆格点?

施药代码:

一次尝试

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) { 
} 
} 

第二次尝试:

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); 
     } 
    } 

第三尝试:

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); 
     } 
    } 

在第三方法我也试图与"/system/bin/su"

回答

1

下面的代码为我工作

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

一个更好的解决方案是运行:

su -c am start -a android.intent.action.ACTION_REQUEST_SHUTDOWN

可以使用Process为此。

相关问题