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相关文章推荐
- 关于Visual Studio项目系统属性中的宏
- java 获取一些项目或系统属性
- Android系统setting中读取apk的属性
- Android读取系统控件的自定义属性
- 读取项目属性文件的几种方式
- 地磅称量系统之(24~26)创建一个名称为WinApp的Windows应用程序作为启动项目并且设置主界面上控件的属性
- Windows系统下基于jenkins+github+fir.im的Android项目持续化集成实例
- WPF项目中读取图片资源,属性为生成操作为Resource
- 凯云水利水电工程造价管理系统 技术解析(三)项目属性(二)
- 换电脑/系统后,,如何同步github上原有的项目呢?...linux下...
- VS打开.net工程提示"无法读取项目文件"web.csproj",系统找不到指定路径"
- Android C 语言读取系统属性
- 软件项目管理系统-项目管理-模块属性编辑
- Java Web项目读取属性文件
- 【入门】创建新项目-系统自动创建了appcompat_v7 res资源属性报错
- 关于Visual Studio项目系统属性中的宏
- 关于Visual Studio项目系统属性中的宏
- visual studio 关于配置项目系统属性宏
- Mac系统 使用github上传项目
- 安卓四大组件-内容提供器-2.1_项目实战-读取系统联系人