Can't reboot device using runtime.exec

时光毁灭记忆、已成空白 提交于 2019-12-10 10:33:27

问题


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

回答1:


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.




回答2:


Finally after weeks of searching:

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



回答3:


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




回答4:


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.




回答5:


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



来源:https://stackoverflow.com/questions/5603221/cant-reboot-device-using-runtime-exec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!