2013-04-18 50 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爲此。

相關問題