Android第三方app获取系统属性
原博客地址(建议去原博客地址看):
https://blog.csdn.net/shadowliucs/article/details/38658155
在一个Android应用中因为要获取系统的属性, 比如说型号, model等一些属性, 通过下列方法就可以获取到.
使用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");
通过以上两种方法基本上可以解决掉该问题了.
Class clazz = Class.forName("android.os.SystemProperties");
Object o = clazz.newInstance();
Method method = clazz.getDeclaredMethod("get", String.class);
String miuiString = (String) method.invoke(o,
"ro.miui.ui.version.name");
if (miuiString != null && !"".equals(miuiString)) {
MIUI = true;
}
String dev = android.os.Build.MANUFACTURER;获取设备名字
Android第三方app获取系统属性相关推荐
- Android 9无法通过反射获取系统属性
Android 9.0之后获取无法通过反射获取系统属性,如果我们需要知道手机某个属性,可以通过以下的方式.注意:这种方式不能获取所有的属性.不然谷歌限制SystemProper的访问就没有意义了. p ...
- Android反射set/get系统属性(SystemProperties)
以Android系统版本号和SDK版本号为例 # setprop ro.build.version.sdk 23 # setprop ro.build.version.release 5.1.1 fr ...
- java获取系统属性_Java获取系统属性
1.System类 The System class contains several useful class fields and methods. It cannot be instantiat ...
- 在Java中获取系统属性
Java语言以其面向对象.跨平台.可移植性好.安全性高等优点,受到众多编程人员的青睐,越来越多的人将其作为应用软件开发语言的首选. 在Java应用程序运行时,特别是需要在跨平台工作环境下运行时,需要确 ...
- android第三方app改为系统app,加入system组,获取system权限
用Androd studio 开发的app,编译出apk, 想获取system权限. 环境:编译好的apk, android 源码环境,有root权限和源码对应的开发板,我这里是user版本. 思路: ...
- android内置第三方APP为系统应用,第三方APP内置到/system/app目录下报错java.lang.UnsatisfiedLinkError,so文件不加载
项目场景: 在无系统签名的情况下,将第三方APP内置到/system/app目录下,可以将第三方应用变为系统应用,用户无法直接卸载.不过前提是手机需要root. 问题描述 使用es文件管理工具将第三方 ...
- Android adb 设置和获取系统属性
/system/build.prop 为系统属性默认值. 安卓系统属性由特殊的property_service管理, /system/build.prop是只读文件, 其中包含property_ser ...
- Android SystemProperties设置/取得系统属性的用法总结
2019独角兽企业重金招聘Python工程师标准>>> 1.Android SystemProperties简介 介绍了设置属性需要的权限,已经设置权限的方法. Systemprop ...
- Android第三方app 微信授权登录
首先呢,说到第三方,大家应该首先想到的是: 1.appid (第三方给的) 2.密钥 (第三方给的) 3.签名文件 4.包名 这4样,往往决定着的是第三方好用不好用!!! 所以呢- 1.申请你的App ...
最新文章
- python 利用jieba读取txt文本进行分词后存入新txt
- 2021 AAAI Fellow名单重磅出炉,华人学者遗憾连续两年无缘入选 | AI日报
- linux命令之上传文件和下载文件
- TCP/IP 原理--链路层
- AWS 专家教你快速使用 Spring Boot 和 DJL!
- Vue项目webpack打包部署到Tomcat,刷新报404错
- 50. 模型层 --- dao 层(2)
- centos6安装mysql并远程连接_Linux中Mysql不支持远程连接解决办法
- C语言程序设计答何钦铭答案,c语言程序设计何钦铭课后题答案
- 关于STM32xE系列芯片STOP模式下使用RTC唤醒所遇到的问题记录
- Mac使用——MongoDB的下载和Compass可视化工具,以及安装过程
- c语言中提取单词首字母,C语言实现单词小帮手
- 睡眠时的局部目标记忆再激活
- 问:平面布置图是什么?有什么作用?如何判断好的平面布置图方案?如何绘制?
- 【第一期】大话计算机网络(猫、路由器、wifi)
- 社保随便挂靠,会判刑罚款您知道吗?
- 学习用Photoshop来设计简单的网页
- 计算机网络钟中PCF,请问,为什么我向时钟芯片PCF8563中写入初始化数据后,读出来...
- 团队开发——极速蜗牛
- 超诡异的自杀案,有点坑爹