android程序执行adb shell命令

文章转载自:http://blog.csdn.net/buptgshengod

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);
        }
    }

  


备注:要操作data/下面的要保证有root权限和读写权限

chmod 777 /data/misc/wifi/wpa_supplicant.conf

可以用下面方法检测是否有root权限
RootTools.isRootAvailable()

如果在PhoneWindowManager.java中写则不需要管权限,他的权限比较高

你可能感兴趣的:(android,学习)