获取系统的节点值

 public  void  show (View  v){
        String readFile = readFile("/sys/class/extertp/data");
        String read = read("/sys/class/extertp/data");
        Toast.makeText(MainActivity.this, read, Toast.LENGTH_SHORT).show();
        Toast.makeText(MainActivity.this, readFile, Toast.LENGTH_SHORT).show();
        String property = System.getProperty("ro.sf.lcd_density");
        Toast.makeText(MainActivity.this, property, Toast.LENGTH_SHORT).show();
    }

    //sys_path 为节点映射到的实际路径
    public static String read(String sys_path){
        try {
            Runtime runtime = Runtime.getRuntime();

            Process process = runtime.exec(" su "+" -c "+ " cat " + sys_path); // 此处进行读操作
            InputStream is = process.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line ;
            while (null != (line = br.readLine())) {
                Log.w(MainActivity.TAG, "read data ---> " + line);
                return line;
            }
        } catch (IOException e) {
            e.printStackTrace();
            Log.w(MainActivity.TAG, "*** ERROR *** Here is what I know: " + e.getMessage());
        }
        return null;
    }


    //sys_path 为节点映射到的实际路径
    public static String readFile(String sys_path) {
        String prop = "waiting";// 默认值
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(sys_path));
            prop = reader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
            Log.w(MainActivity.TAG, " ***ERROR*** Here is what I know: " + e.getMessage());
        } finally {
            if(reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        Log.w(MainActivity.TAG, "readFile cmd from"+sys_path + "data"+" -> prop = "+prop);
        return prop;
    }


    public static void writeValue(String filename, String value) {
        //FileOutputStream fos = null;
        DataOutputStream os = null;
        try {
        /*fos = new FileOutputStream(new File(filename), false);
        fos.write(value.getBytes());
        fos.flush();*/

            Process p;

            p = Runtime.getRuntime().exec("su");
            os = new DataOutputStream(p.getOutputStream());

            os.writeBytes(value + " > " + filename + "\n");
            os.writeBytes("exit\n");

            Log.w(TAG, value + " >> " + filename);

            os.flush();
        } catch (IOException e) {
            Log.w(TAG, "Could not write to " + filename);
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    Log.d(TAG, "Could not close " + filename, e);
                }
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值