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); } } } } }
获取系统的节点值
最新推荐文章于 2021-05-31 04:21:35 发布