
android開發中,我們經常需要讀取系統屬性值,這個讀取系統屬性值的方法有許多: (1)手機連接電腦,在電腦終端中輸入:adb shell getPRop **,就可以讀取到對應的系統屬性。 此方法需要將手機連接電話,并且需要將手機的USB 調試功能打開,這其實就影響到了對應的USB相關的系統屬性。
(2)Systemproperties類來實現 Systemproperties.set(name, value) Systemproperties.get(name) 這二個方法可以實現對系統屬性讀取和操作,但是可惜啊,此方法需要在AndroidManifest.xml中,加入android:sharedUserId=”android.uid.system”。 并且在Android.mk中,將LOCAL_CERTIFICATE := XXX修改成LOCAL_CERTIFICATE :=platform。 經過以上兩步就可以把ap的權限提升到system權限了。
但是用這種方法: 1、程序的擁有者必須有程序的源碼; 2、程序的擁有者還必須有android開發環境,就是說自己能make整個android系統。
這一般的應用開發者,都是不具備此條件的。
此方法是使用/system/bin/getprop,來實現系統屬性的讀取操作,可以不需要連接電腦,不需要打開USB調試,不需要提高開發者權限到system,像正常的應用一樣,直接顯示系統屬性。
(1)讀取系統屬性
private String commandPro ="/system/bin/getprop";private static final String OUTPUT_FILE = Environment.getExternalStorageDirectory().toString() + "/prop_all.txt";//執行讀取系統屬性的命令restul = PropHelper.execCommand(command);public static String execCommand(String command) throws IOException { String result = "fail02"; Runtime runtime = Runtime.getRuntime(); Process proc = runtime.exec(command); try { if (proc.waitFor() != 0) { System.err.println("exit value = " + proc.exitValue()); return result; } BufferedReader in = new BufferedReader( new InputStreamReader(proc.getInputStream())); StringBuffer stringBuffer = new StringBuffer(); String line = null; while ((line = in.readLine()) != null) { stringBuffer.append(line+" /n"); } result = stringBuffer.toString(); return result; } catch (InterruptedException e) { System.err.println(e); return "fail03"; } }(2)將讀取的系統屬性保存到SD卡
//將讀取的系統屬性保存到SD卡boolean success = PropHelper.saveToSDCard(restul,OUTPUT_FILE);public static boolean saveToSDCard(String content,String des) { boolean result = true; FileOutputStream fos = null; try { File f1 = new File(des); if(f1.exists()){ f1.delete(); } f1.createNewFile(); fos = new FileOutputStream(f1); fos.write(content.getBytes()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); result = false; return result; }finally{ try { if(fos != null){ fos.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); result = false; return result; } } return result; }https://github.com/hfreeman2008/AndroidDevelopHelper
新聞熱點
疑難解答