Android 執行adb 並返回值

public static boolean ExuseCmd(String cmdStr){
        Process process = null;
        DataOutputStream os = null;
        try {
            process = Runtime.getRuntime().exec("su");
            os = new DataOutputStream(process.getOutputStream());
            os.writeBytes(cmdStr + "\n");
            os.writeBytes("exit\n");
            os.flush();

            //獲取返回值
            InputStream is = process.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
         	StringBuffer strb = new StringBuffer();
            String line;
            while ((line = reader.readLine()) != null) {
                strb.append(line + "\n");
                Log.d("cmd",""+strb);
            }
            is.close();
            reader.close();
            process.waitFor();
            process.destroy();
        } catch (Exception e) {
            return false;
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (process != null) {
                    process.destroy();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
         return true;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章