android程序执行adb shell命令

来源:互联网 发布:如何下载linux系统 编辑:程序博客网 时间:2024/05/18 06:19

文章转载自: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);        }    }

<uses-permission android:name="android.permission.INTERNET"></uses-permission>  


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

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

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

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