您的位置:首页 > 编程语言

github项目之读取系统属性

2017-02-28 14:57 393 查看


前言

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实现系统属性的读取操作

此方法是使用/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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息