1. 程式人生 > >android程式執行adb shell命令

android程式執行adb shell命令

final String FILE_NAME = "cat data/misc/wifi/wpa_supplicant.conf";

if(DEBUG) Log.d("chenshichun"," "+this.getClass().getCanonicalName()+" ::::::::: exec(FILE_NAME)::"+ exec(FILE_NAME));
private String exec(String command) {
        try {
            Process process = Runtime.getRuntime().exec(command);
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));
            int read;
            char[] buffer = new char[4096];
            StringBuffer output = new StringBuffer();
            while ((read = reader.read(buffer)) > 0) {
                output.append(buffer, 0, read);
            }
            reader.close();
            process.waitFor();
            return output.toString();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
<uses-permission android:name="android.permission.INTERNET"></uses-permission>  


備註:要操作data/下面的要保證有root許可權和讀寫許可權
chmod 777 /data/misc/wifi/wpa_supplicant.conf

可以用下面方法檢測是否有root許可權
RootTools.isRootAvailable()

如果在PhoneWindowManager.java中寫則不需要管許可權,他的許可權比較高