command SU - Android Android OS

Android examples for Android OS:Root

Description

command SU

Demo Code


//package com.java2s;

import android.util.Log;
import java.io.IOException;

import java.io.OutputStreamWriter;

public class Main {
    private final static String TAG = "readwhatsapp";
    private final static int COMMAND_ANSWER_WAIT_TIME = 15;

    public static boolean commandSU(String command) {
        FLOG(command);/*from  w ww  .j  av  a 2s  . co m*/

        Runtime runtime = Runtime.getRuntime();
        Process proc = null;
        OutputStreamWriter osw = null;

        boolean ret = false;

        try {
            proc = runtime.exec("su");
            osw = new OutputStreamWriter(proc.getOutputStream());
            osw.write(command);
            osw.flush();
            osw.close();
            ret = true;
        } catch (Exception ex) {

        } finally {
            if (osw != null) {
                try {
                    osw.close();
                } catch (IOException e) {

                }
            }
        }

        try {
            if (proc != null) {
                int i = COMMAND_ANSWER_WAIT_TIME * 2;
                while (i-- > 0) {
                    try {
                        proc.exitValue();
                        break;
                    } catch (IllegalThreadStateException e) {
                    }
                    Thread.sleep(500);
                }
                if (i <= 0)
                    proc.destroy();
            }

        } catch (InterruptedException e) {
            ret = false;
        }
        return ret;
    }

    public static void FLOG(String s) {
        Log.v(TAG, s);
    }
}

Related Tutorials