开发者

Can't reboot device using runtime.exec

For some reason I cannot reboot Android devices using Runtime.getRuntime().exec("/system/bin/reboot");. I have tried the following code on 3 devices now without luck. One was built from rowboat-android source. The other two are the Motorola Droid Pro (Rooted, stock) and the HTC Ledgent (Rooted, Cynogen Mod). All devices are running Android 2.2 Froyo.

Does anyone know why? su works as well as the Super User application is visible. I should note various other shell commands do work, like netcfg (chmod' to 777) and ls.

public static boolean executeCommand(SHELL_CMD iShellCmd){
        Stri开发者_JAVA百科ng cmd = null;
        String line = null;
        String fullResponse = null;
        Process lPs = null;

        Log.d(Global.TAG,"--> !!!!! Running shell command");

        if (iShellCmd == SHELL_CMD.restart_device){
            cmd = "reboot";
        }

        Log.d(Global.TAG,"--> About to exec: " + cmd);

        try {
            lPs = Runtime.getRuntime().exec("su");
            lPs = Runtime.getRuntime().exec("/system/bin/reboot");
        } catch (Exception e) {
            e.printStackTrace();
        }

        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(lPs.getOutputStream()));
        BufferedReader in = new BufferedReader(new InputStreamReader(lPs.getInputStream()));

        try {
            while ((line = in.readLine()) != null) {
                Log.d(Global.TAG,"--> Command result line: " + line);
                if (fullResponse == null){
                    fullResponse = line;
                }else{
                    fullResponse = fullResponse + "\n" + line;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        Log.d(Global.TAG,"--> Full response was: " + fullResponse);

        return true;
    }


Depending on how you've obtained root permission on your device, you can do any of the following:

Runtime.getRuntime().exec(new String[]{"/system/xbin/su","-c","reboot"});

or

Runtime.getRuntime().exec(new String[]{"/system/bin/su","-c","reboot"});

or

Runtime.getRuntime().exec(new String[]{"su","-c","reboot"});

Probably better to test all three scenarios in your application.


Finally after weeks of searching:

Runtime.getRuntime().exec(new String[]{"/system/bin/su","-c","reboot now"});


Try running "su /system/bin/reboot" instead of su and the command on different lines. That might help :)


If you want a button to be pressed try:

final Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
             try {
                 Runtime.getRuntime().exec(new String[]{"/system/bin/su","-c","reboot"});              
            } catch (IOException e) {
            }               
        }
    });

By the way for this code to work the button has to be called button1.


instead of {"/system/bin/su","-c","reboot"} i changed the "/system/bin/su" part to just "su" and then it worked for me.

Like this Runtime.getRuntime().exec(new String[]{"su","-c","reboot"});

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜