Newer
Older
KillProcess / app / src / main / java / me / bell / killprocess / RootUtil.java
bello on 27 Mar 2018 1 KB init
package me.bell.killprocess;

import android.util.Log;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @Info
 * @Auth Bello
 * @Time 18/3/24 下午11:29
 * @Ver
 */

public class RootUtil {
    static String TAG = "RootUtil";

    public static void KillProcess(String cmd){
        try {
            String s = "\n";
            String sErr = "\n";
            Log.d(TAG,"cmd=" + cmd);

            Process p = Runtime.getRuntime().exec(cmd);

            InputStream is = p.getInputStream();
            InputStream ers = p.getErrorStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            DataInputStream ise = new DataInputStream(ers);
            String line = null;
            String error = null;
            while ((line = reader.readLine()) != null) {
                s += line + "\n";
            }
            Log.d(TAG,"s=" + s);
            while((error = ise.readLine()) != null) {
                sErr += error + "\n";
            }
            Log.d(TAG,"sErr=" + sErr);
            int result = p.waitFor();
            Log.d(TAG,"result=" + result);

            is.close();
            ers.close();
            reader.close();
            ise.close();
            p.destroy();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}