android 读取系统文件 wpa_supplicant

1,须要权限

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

2,下载 RootTools.jar包。

3。两个关键方法。

主要是获取shell,并运行命令行。

方法例如以下:

  private static boolean waitForCommand(Command cmd) {
        while (!cmd.isFinished()) {
            synchronized (cmd) {
                try {
                    if (!cmd.isFinished()) {
                        cmd.wait(2000);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            if (!cmd.isExecuting() && !cmd.isFinished()) {
                //         Logger.errorST("Error: Command is not executing and is not finished!");
                return false;
            }
        }

        //Logger.debug("Command Finished!");
        return true;
    }
    public static ArrayList<String> runAndWait1(String cmd, final boolean root) {
        final ArrayList<String> output = new ArrayList<String>();
        Command cc = new Command(1, cmd) {
            @Override
            public void commandOutput(int i, String s) {
                output.add(s);
//        System.out.println("output "+root+s);
            }

            @Override
            public void commandTerminated(int i, String s) {

                System.out.println("error" + root + s);

            }

            @Override
            public void commandCompleted(int i, int i2) {

            }
        };
        try {
            RootTools.getShell(root).add(cc);
        } catch (Exception e) {
            //       Logger.errorST("Exception when trying to run shell command", e);
            e.printStackTrace();
            return null;
        }

        if (!waitForCommand(cc)) {
            return null;
        }

        return output;
    }


4,接下来就是简单的调用了。

final  File f=new File("/data/misc/wifi/wpa_supplicant.conf");

 new Thread(){
     @Override
     public void run() {
         super.run();
         ArrayList<String> list=new ArrayList<String>();
       //  String cpath = getCommandLineString(f.getPath());
         String s="cat " + f.getPath();
         list = runAndWait1(s, true);
         for (int i = 0; i < list.size(); i++) {
             Log.e("content",list.get(i));
         }


     }
 }.start();


输出结果例如以下:




posted @ 2017-08-19 15:26  zsychanpin  阅读(1122)  评论(0编辑  收藏  举报