zoukankan      html  css  js  c++  java
  • 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();


    输出结果例如以下:




  • 相关阅读:
    手脱ASPack v2.12变形壳2
    手脱nSPack 2.1
    WCF分布式开发步步为赢(1):WCF分布式框架基础概念
    一个经典例子让你彻彻底底理解java回调机制
    C#三层架构详细解剖
    eclipse快捷键及各种设置
    设计模式总结
    程序猿也爱学英语(上)
    关于PDA、GPS等动态资源的几种GIS解决方案
    通过VS2010性能分析来查找代码中那些地方最损耗资源
  • 原文地址:https://www.cnblogs.com/zsychanpin/p/7396731.html
Copyright © 2011-2022 走看看