android获取ro._Android应用获取系统属性
使用adb shell prop可以查看Android系统的属性.详情看下图
上面列出了很多属性, 如果要在使用中使用该属性, 比如说我是为了判断该手机是不是魅族手机.
google在framework层提供了一个SystemProperties类, 该类位于框架层, 具体位置是frameworks/base/core/java/android/os/SystemProperties.java
该类提供了很详细的访问各种属性的方法,所以我可以使用反射机制来获取我想要的参数.
package me.chasel.utils
import java.io.File;
import java.lang.reflect.Method;
import android.content.Context;
import dalvik.system.DexFile;
public class SystemPropertiesProxy
{
/**
* 根据给定Key获取值.
* @return 如果不存在该key则返回空字符串
* @throws IllegalArgumentException 如果key超过32个字符则抛出该异常
*/
public static String get(Context context, String key) throws IllegalArgumentException {
String ret= "";
try{
ClassLoader cl = context.getClassLoader();
@SuppressWarnings("rawtypes")
Class SystemProperties = cl.loadClass("android.os.SystemProperties");
//参数类型
@SuppressWarnings("rawtypes")
Class[] paramTypes= new Class[1];
paramTypes[0]= String.class;
Method get = SystemProperties.getMethod("get", paramTypes);
//参数
Object[] params= new Object[1];
params[0]= new String(key);
ret= (String) get.invoke(SystemProperties, params);
}catch( IllegalArgumentException iAE ){
throw iAE;
}catch( Exception e ){
ret= "";
//TODO
}
return ret;
}
/**
* 根据Key获取值.
* @return 如果key不存在, 并且如果def不为空则返回def否则返回空字符串
* @throws IllegalArgumentException 如果key超过32个字符则抛出该异常
*/
public static String get(Context context, String key, String def) throws IllegalArgumentException {
String ret= def;
try{
ClassLoader cl = context.getClassLoader();
@SuppressWarnings("rawtypes")
Class SystemProperties = cl.loadClass("android.os.SystemProperties");
//参数类型
@SuppressWarnings("rawtypes")
Class[] paramTypes= new Class[2];
paramTypes[0]= String.class;
paramTypes[1]= String.class;
Method get = SystemProperties.getMethod("get", paramTypes);
//参数
Object[] params= new Object[2];
params[0]= new String(key);
params[1]= new String(def);
ret= (String) get.invoke(SystemProperties, params);
}catch( IllegalArgumentException iAE ){
throw iAE;
}catch( Exception e ){
ret= def;
//TODO
}
return ret;
}
/**
* 根据给定的key返回int类型值.
* @param key 要查询的key
* @param def 默认返回值
* @return 返回一个int类型的值, 如果没有发现则返回默认值
* @throws IllegalArgumentException 如果key超过32个字符则抛出该异常
*/
public static Integer getInt(Context context, String key, int def) throws IllegalArgumentException {
Integer ret= def;
try{
ClassLoader cl = context.getClassLoader();
@SuppressWarnings("rawtypes")
Class SystemProperties = cl.loadClass("android.os.SystemProperties");
//参数类型
@SuppressWarnings("rawtypes")
Class[] paramTypes= new Class[2];
paramTypes[0]= String.class;
paramTypes[1]= int.class;
Method getInt = SystemProperties.getMethod("getInt", paramTypes);
//参数
Object[] params= new Object[2];
params[0]= new String(key);
params[1]= new Integer(def);
ret= (Integer) getInt.invoke(SystemProperties, params);
}catch( IllegalArgumentException iAE ){
throw iAE;
}catch( Exception e ){
ret= def;
//TODO
}
return ret;
}
/**
* 根据给定的key返回long类型值.
* @param key 要查询的key
* @param def 默认返回值
* @return 返回一个long类型的值, 如果没有发现则返回默认值
* @throws IllegalArgumentException 如果key超过32个字符则抛出该异常
*/
public static Long getLong(Context context, String key, long def) throws IllegalArgumentException {
Long ret= def;
try{
ClassLoader cl = context.getClassLoader();
@SuppressWarnings("rawtypes")
Class SystemProperties= cl.loadClass("android.os.SystemProperties");
//参数类型
@SuppressWarnings("rawtypes")
Class[] paramTypes= new Class[2];
paramTypes[0]= String.class;
paramTypes[1]= long.class;
Method getLong = SystemProperties.getMethod("getLong", paramTypes);
//参数
Object[] params= new Object[2];
params[0]= new String(key);
params[1]= new Long(def);
ret= (Long) getLong.invoke(SystemProperties, params);
}catch( IllegalArgumentException iAE ){
throw iAE;
}catch( Exception e ){
ret= def;
//TODO
}
return ret;
}
/**
* 根据给定的key返回boolean类型值.
* 如果值为 'n', 'no', '0', 'false' or 'off' 返回false.
* 如果值为'y', 'yes', '1', 'true' or 'on' 返回true.
* 如果key不存在, 或者是其它的值, 则返回默认值.
* @param key 要查询的key
* @param def 默认返回值
* @return 返回一个boolean类型的值, 如果没有发现则返回默认值
* @throws IllegalArgumentException 如果key超过32个字符则抛出该异常
*/
public static Boolean getBoolean(Context context, String key, boolean def) throws IllegalArgumentException {
Boolean ret= def;
try{
ClassLoader cl = context.getClassLoader();
@SuppressWarnings("rawtypes")
Class SystemProperties = cl.loadClass("android.os.SystemProperties");
//参数类型
@SuppressWarnings("rawtypes")
Class[] paramTypes= new Class[2];
paramTypes[0]= String.class;
paramTypes[1]= boolean.class;
Method getBoolean = SystemProperties.getMethod("getBoolean", paramTypes);
//参数
Object[] params= new Object[2];
params[0]= new String(key);
params[1]= new Boolean(def);
ret= (Boolean) getBoolean.invoke(SystemProperties, params);
}catch( IllegalArgumentException iAE ){
throw iAE;
}catch( Exception e ){
ret= def;
//TODO
}
return ret;
}
/**
* 根据给定的key和值设置属性, 该方法需要特定的权限才能操作.
* @throws IllegalArgumentException 如果key超过32个字符则抛出该异常
* @throws IllegalArgumentException 如果value超过92个字符则抛出该异常
*/
public static void set(Context context, String key, String val) throws IllegalArgumentException {
try{
@SuppressWarnings("unused")
DexFile df = new DexFile(new File("/system/app/Settings.apk"));
@SuppressWarnings("unused")
ClassLoader cl = context.getClassLoader();
@SuppressWarnings("rawtypes")
Class SystemProperties = Class.forName("android.os.SystemProperties");
//参数类型
@SuppressWarnings("rawtypes")
Class[] paramTypes= new Class[2];
paramTypes[0]= String.class;
paramTypes[1]= String.class;
Method set = SystemProperties.getMethod("set", paramTypes);
//参数
Object[] params= new Object[2];
params[0]= new String(key);
params[1]= new String(val);
set.invoke(SystemProperties, params);
}catch( IllegalArgumentException iAE ){
throw iAE;
}catch( Exception e ){
//TODO
}
}
}
这是第一种方法, 这也是一种万能的方法.
当然如果不是非常必要的话, 建议还是不要使用这样的万能方法.
第二种方法是其它如果只是要简单的判断是否为魅族手机的话, 完全可以调用google提供的api来做.
android.os.Build.BRAND
使用上面的属性如果是魅族手机的话返回返回Meizu.
这是和我使用adb shell prop看到的属性是一致的, 可以看下图
看到该类的源码也可以看到如下内容
public class Build {
/** Value used for when a build property is unknown. */
public static final String UNKNOWN = "unknown";
/** Either a changelist number, or a label like "M4-rc20". */
public static final String ID = getString("ro.build.id");
/** A build ID string meant for displaying to the user */
public static final String DISPLAY = getString("ro.build.display.id");
/** The name of the overall product. */
public static final String PRODUCT = getString("ro.product.name");
/** The name of the industrial design. */
public static final String DEVICE = getString("ro.product.device");
/** The name of the underlying board, like "goldfish". */
public static final String BOARD = getString("ro.product.board");
/** The name of the instruction set (CPU type + ABI convention) of native code. */
public static final String CPU_ABI = getString("ro.product.cpu.abi");
/** The name of the second instruction set (CPU type + ABI convention) of native code. */
public static final String CPU_ABI2 = getString("ro.product.cpu.abi2");
/** The manufacturer of the product/hardware. */
public static final String MANUFACTURER = getString("ro.product.manufacturer");
/** The brand (e.g., carrier) the software is customized for, if any. */
public static final String BRAND = getString("ro.product.brand");
/** The end-user-visible name for the end product. */
public static final String MODEL = getString("ro.product.model");
/** The system bootloader version number. */
public static final String BOOTLOADER = getString("ro.bootloader");
通过以上两种方法基本上可以解决掉该问题了.
android获取ro._Android应用获取系统属性相关推荐
- android获取ro._Android 简单的设备信息获取
在用python写自动化测试脚本的脚本的时候通常回需要获取一些android设备的硬件信息.网上给的很多方案都是通常通过/system/build.prop中去获取,但是回遇到permission d ...
- android获取ro._Android群控黑盒调用 - Sekiro食用手册
0x0 前言 之前尝试用过virjar大佬的hermesagent, 后来大佬又迭代出新的基于长链接的Sekiro, 一直想看都被耽搁了, 今天正好抽空尝试一下, 顺便写篇笔记, 有错误的地方大佬们请 ...
- java实时获取android网速_Android中获取实时网速(2)
一.实现思路: 1.Android提供有获取当前总流量的方法 2.上一秒 减去 下一面的流量差便是网速 3.注意计算 二.计算网速的工具类: package imcs.cb.com.viewappli ...
- android gps 获取方位_Android GPS获取当前经纬度坐标
APP中可能会遇到一种需求,就是将当前所在位置的坐标传到服务器上,今天我提供三种途径去获取经纬度坐标信息,第一种是通过Android API来实现,第二种通过百度地图API来实现,第三种通过天地图AP ...
- android java 时间_android java获取当前时间的总结
Java 代码 importjava.text.SimpleDateFormat; SimpleDateFormat formatter = newSimpleDateFormat ( ...
- android dts配置_Android 4.0系统支持DTS音效_华为手机_手机Android频道-中关村在线
在系统方面,华为T8950荣耀+采用的是当前新机比较主流的Android 4.0系统.虽然使用的是原生Android 4.0系统,但是华为在此基础上还是做了一定的优化.从主界面来看,并没有太多的特色. ...
- 【Android 逆向】Android 权限 ( ro.product.cpu.abi 属性 | ro.zygote 属性 | dhcp.eth0 属性 | net.* 属性 )
文章目录 一.Android 权限相关的重要的系统属性 1.ro.product.cpu.abi 属性 2.ro.zygote 属性 3.dhcp.eth0 属性 4.net.* 属性 一.Andro ...
- 使用Java代码在应用层获取Android系统属性
之前使用Native代码的property_get()/property_set()来获取Android系统属性,现在需要改写到Java上面, 但是System.getProperty() / Sys ...
- Android : 反射机制获取或设置系统属性(SystemProperties)
Android.os.SystemProperties 提供了获取和设置系统属性的方法,但是这个类被隐藏了,应用开发时无法直接访问,可以通过反射的机制进行操作. 获取系统属性 public stati ...
最新文章
- kali扫描内网ip_来,我们聊聊内网渗透!
- 你写的接口都测试吗?测什么?怎么测?
- mysql数据库分表备份脚本_MySQL分库分表备份脚本
- java实现红包要多少钱_Java实现发红包功能
- CPP函数调用的方法
- Java枚举类型 enum
- synchronized方法与synchronized代码块的区别
- Vue 学习笔记(4)Vue-cli4 项目搭建 + 目录结构 + 项目打包、部署
- ORACLE EBS FORM 二次开发常用小技巧
- 如何写论文?看下这份《科研论文撰写策略》为你指点一二
- win7安装电子专利CPC客户端以及专利文件的生成过程
- matlab解隐式差分格式,【毕业设计(论文)】二维热传导方程有限差分法的MATLAB实现...
- java 支付宝 H5支付
- c语言编程中的大于号怎么打,excel表格中怎么输入比较运算符号(大于、小于号)?...
- modelsim error:iteration limit reached at time xxx ns.
- 机器学习基石-05-1-Recap and Preview
- docker 构建推送到阿里云仓库失败
- python asyncio_python中asyncio模块
- ubuntu22从双系统开始到深度学习环境搭建+必备软件安装
- 如何使用arcmap转换shp文件坐标系