run Reboot command - Android Android OS

Android examples for Android OS:Shell Command

Description

run Reboot command

Demo Code


import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import com.imalur.battery100discharger.R;
import android.content.Context;
import android.widget.Toast;

public class Main{
    /*from  ww  w  . j a  v a 2 s .  c o  m*/
    public static boolean runRebootcmd(Context context) {
        String[] cmds = { Commands.REBOOT };
        try {
            RootUtils.runAsRoot(cmds);
        } catch (IOException e) {
            Toast.makeText(context, R.string.toast_error, Toast.LENGTH_LONG)
                    .show();
            return false;
        }
        Toast.makeText(context, R.string.toast_reboot, Toast.LENGTH_LONG)
                .show();
        return true;
    }
    /**
     * Execute terminal commands on rooted device
     * @param cmds
     * @throws IOException
     */
    public static void runAsRoot(String[] cmds) throws IOException {
        Process p = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(p.getOutputStream());
        for (String tmpCmd : cmds) {
            os.writeBytes(tmpCmd + "\n");
        }
        os.writeBytes("exit\n");
        os.flush();
    }
}

Related Tutorials