Android SystemProperties【学习笔记】
2017-02-22 本文已影响1105人
爱下雪的孩子
一、第一种方式代码直接调用SystemProperties API
1.Android Studio 首先需要导入layoutlib.jar如下:
在Build.gradle下加:
def getLayoutLibPath() {
return "${android.getSdkDirectory().getAbsolutePath()}" +
"/platforms/"+android.compileSdkVersion + "/data/layoutlib.jar"
}
dependencies {
provided files(getLayoutLibPath())
}
调用:【例】String SerialNumber = android.os.SystemProperties.get("ro.serialno"); 获取系统S/N码
二、第二种方式通过android.os.Build能获取的设备信息:
android.os.Build.BOARD:获取设备基板名称
android.os.Build.BOOTLOADER:获取设备引导程序版本号
android.os.Build.BRAND:获取设备品牌
android.os.Build.CPU_ABI:获取设备指令集名称(CPU的类型)
android.os.Build.CPU_ABI2:获取第二个指令集名称
android.os.Build.DEVICE:获取设备驱动名称
android.os.Build.DISPLAY:获取设备显示的版本包(在系统设置中显示为版本号)和ID一样
android.os.Build.FINGERPRINT:设备的唯一标识。由设备的多个信息拼接合成。
android.os.Build.HARDWARE:设备硬件名称,一般和基板名称一样(BOARD)
android.os.Build.HOST:设备主机地址
android.os.Build.ID:设备版本号。
android.os.Build.MODEL :获取手机的型号 设备名称。
android.os.Build.MANUFACTURER:获取设备制造商
android:os.Build.PRODUCT:整个产品的名称
android:os.Build.RADIO:无线电固件版本号,通常是不可用的 显示unknown
android.os.Build.TAGS:设备标签。如release-keys 或测试的 test-keys
android.os.Build.TIME:时间
android.os.Build.TYPE:设备版本类型 主要为"user" 或"eng".
android.os.Build.USER:设备用户名 基本上都为android-build
android.os.Build.VERSION.RELEASE:获取系统版本字符串。如4.1.2 或2.2 或2.3等
android.os.Build.VERSION.CODENAME:设备当前的系统开发代号,一般使用REL代替
android.os.Build.VERSION.INCREMENTAL:系统源代码控制值,一个数字或者git hash值
android.os.Build.VERSION.SDK:系统的API级别 一般使用下面大的SDK_INT 来查看
android.os.Build.VERSION.SDK_INT:系统的API级别 数字表示
android.os.Build.VERSION_CODES类 中有所有的已公布的Android版本号。全部是Int常亮。可用于与SDK_INT进行比较来判断当前的系统版本
调用:【例】String SerialNumber = android.os.Build.SERIAL;获取系统S/N码
三、第三种方式通过反射来获取:
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
}
}
}
调用:【例】String SerialNumber = SystemPropertiesProxy.get(MainActivity.this, "ro.serialno");获取系统S/N码