• 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();


    输出结果例如以下:




  • 相关阅读:
    jquery下拉菜单打开的同时,同行右边的图标变化
    echarts引入及应用
    好用又美观的时间控件
    C#不支持此安全协议
    python re模块中的函数
    python中的收集参数
    python常用操作符
    python 字符串中常用的内置函数
    VS2012停止工作解决办法
    Jqurey图片放大镜插件
  • 原文地址:https://www.cnblogs.com/zsychanpin/p/7396731.html
Copyright © 2020-2023  润新知