原博客地址(建议去原博客地址看):

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

该类提供了很详细的访问各种属性的方法,所以我可以使用反射机制来获取我想要的参数.

  1. package me.chasel.utils

  2. import java.io.File;

  3. import java.lang.reflect.Method;

  4. import android.content.Context;

  5. import dalvik.system.DexFile;

  6. public class SystemPropertiesProxy

  7. {

  8.     /**

  9.      * 根据给定Key获取值.

  10.      * @return 如果不存在该key则返回空字符串

  11.      * @throws IllegalArgumentException 如果key超过32个字符则抛出该异常

  12.      */

  13.     public static String get(Context context, String key) throws IllegalArgumentException {

  14.         String ret= "";

  15.         try{

  16.           ClassLoader cl = context.getClassLoader(); 

  17.           @SuppressWarnings("rawtypes")

  18.           Class SystemProperties = cl.loadClass("android.os.SystemProperties");

  19.           //参数类型

  20.           @SuppressWarnings("rawtypes")

  21.               Class[] paramTypes= new Class[1];

  22.           paramTypes[0]= String.class;

  23.           Method get = SystemProperties.getMethod("get", paramTypes);

  24.           //参数

  25.           Object[] params= new Object[1];

  26.           params[0]= new String(key);

  27.           ret= (String) get.invoke(SystemProperties, params);

  28.         }catch( IllegalArgumentException iAE ){

  29.             throw iAE;

  30.         }catch( Exception e ){

  31.             ret= "";

  32.             //TODO

  33.         }

  34.         return ret;

  35.     }

  36.     /**

  37.      * 根据Key获取值.

  38.      * @return 如果key不存在, 并且如果def不为空则返回def否则返回空字符串

  39.      * @throws IllegalArgumentException 如果key超过32个字符则抛出该异常

  40.      */

  41.     public static String get(Context context, String key, String def) throws IllegalArgumentException {

  42.         String ret= def;

  43.         try{

  44.           ClassLoader cl = context.getClassLoader(); 

  45.           @SuppressWarnings("rawtypes")

  46.           Class SystemProperties = cl.loadClass("android.os.SystemProperties");

  47.           //参数类型

  48.           @SuppressWarnings("rawtypes")

  49.               Class[] paramTypes= new Class[2];

  50.           paramTypes[0]= String.class;

  51.           paramTypes[1]= String.class;          

  52.           Method get = SystemProperties.getMethod("get", paramTypes);

  53.           //参数

  54.           Object[] params= new Object[2];

  55.           params[0]= new String(key);

  56.           params[1]= new String(def);

  57.           ret= (String) get.invoke(SystemProperties, params);

  58.         }catch( IllegalArgumentException iAE ){

  59.             throw iAE;

  60.         }catch( Exception e ){

  61.             ret= def;

  62.             //TODO

  63.         }

  64.         return ret;

  65.     }

  66.     /**

  67.      * 根据给定的key返回int类型值.

  68.      * @param key 要查询的key

  69.      * @param def 默认返回值

  70.      * @return 返回一个int类型的值, 如果没有发现则返回默认值

  71.      * @throws IllegalArgumentException 如果key超过32个字符则抛出该异常

  72.      */

  73.     public static Integer getInt(Context context, String key, int def) throws IllegalArgumentException {

  74.         Integer ret= def;

  75.         try{

  76.           ClassLoader cl = context.getClassLoader(); 

  77.           @SuppressWarnings("rawtypes")

  78.           Class SystemProperties = cl.loadClass("android.os.SystemProperties");

  79.           //参数类型

  80.           @SuppressWarnings("rawtypes")

  81.               Class[] paramTypes= new Class[2];

  82.           paramTypes[0]= String.class;

  83.           paramTypes[1]= int.class;  

  84.           Method getInt = SystemProperties.getMethod("getInt", paramTypes);

  85.           //参数

  86.           Object[] params= new Object[2];

  87.           params[0]= new String(key);

  88.           params[1]= new Integer(def);

  89.           ret= (Integer) getInt.invoke(SystemProperties, params);

  90.         }catch( IllegalArgumentException iAE ){

  91.             throw iAE;

  92.         }catch( Exception e ){

  93.             ret= def;

  94.             //TODO

  95.         }

  96.         return ret;

  97.     }

  98.     /**

  99.      * 根据给定的key返回long类型值.

  100.      * @param key 要查询的key

  101.      * @param def 默认返回值

  102.      * @return 返回一个long类型的值, 如果没有发现则返回默认值

  103.      * @throws IllegalArgumentException 如果key超过32个字符则抛出该异常

  104.      */

  105.     public static Long getLong(Context context, String key, long def) throws IllegalArgumentException {

  106.         Long ret= def;

  107.         try{

  108.           ClassLoader cl = context.getClassLoader();

  109.           @SuppressWarnings("rawtypes")

  110.               Class SystemProperties= cl.loadClass("android.os.SystemProperties");

  111.           //参数类型

  112.           @SuppressWarnings("rawtypes")

  113.               Class[] paramTypes= new Class[2];

  114.           paramTypes[0]= String.class;

  115.           paramTypes[1]= long.class;  

  116.           Method getLong = SystemProperties.getMethod("getLong", paramTypes);

  117.           //参数

  118.           Object[] params= new Object[2];

  119.           params[0]= new String(key);

  120.           params[1]= new Long(def);

  121.           ret= (Long) getLong.invoke(SystemProperties, params);

  122.         }catch( IllegalArgumentException iAE ){

  123.             throw iAE;

  124.         }catch( Exception e ){

  125.             ret= def;

  126.             //TODO

  127.         }

  128.         return ret;

  129.     }

  130.     /**

  131.      * 根据给定的key返回boolean类型值.

  132.      * 如果值为 'n', 'no', '0', 'false' or 'off' 返回false.

  133.      * 如果值为'y', 'yes', '1', 'true' or 'on' 返回true.

  134.      * 如果key不存在, 或者是其它的值, 则返回默认值.

  135.      * @param key 要查询的key

  136.      * @param def 默认返回值

  137.      * @return 返回一个boolean类型的值, 如果没有发现则返回默认值

  138.      * @throws IllegalArgumentException 如果key超过32个字符则抛出该异常

  139.      */

  140.     public static Boolean getBoolean(Context context, String key, boolean def) throws IllegalArgumentException {

  141.         Boolean ret= def;

  142.         try{

  143.           ClassLoader cl = context.getClassLoader(); 

  144.           @SuppressWarnings("rawtypes")

  145.           Class SystemProperties = cl.loadClass("android.os.SystemProperties");

  146.           //参数类型

  147.           @SuppressWarnings("rawtypes")

  148.               Class[] paramTypes= new Class[2];

  149.           paramTypes[0]= String.class;

  150.           paramTypes[1]= boolean.class;  

  151.           Method getBoolean = SystemProperties.getMethod("getBoolean", paramTypes);

  152.           //参数     

  153.           Object[] params= new Object[2];

  154.           params[0]= new String(key);

  155.           params[1]= new Boolean(def);

  156.           ret= (Boolean) getBoolean.invoke(SystemProperties, params);

  157.         }catch( IllegalArgumentException iAE ){

  158.             throw iAE;

  159.         }catch( Exception e ){

  160.             ret= def;

  161.             //TODO

  162.         }

  163.         return ret;

  164.     }

  165.     /**

  166.      * 根据给定的key和值设置属性, 该方法需要特定的权限才能操作.

  167.      * @throws IllegalArgumentException 如果key超过32个字符则抛出该异常

  168.      * @throws IllegalArgumentException 如果value超过92个字符则抛出该异常

  169.      */

  170.     public static void set(Context context, String key, String val) throws IllegalArgumentException {

  171.         try{

  172.           @SuppressWarnings("unused")

  173.           DexFile df = new DexFile(new File("/system/app/Settings.apk"));

  174.           @SuppressWarnings("unused")

  175.           ClassLoader cl = context.getClassLoader(); 

  176.           @SuppressWarnings("rawtypes")

  177.           Class SystemProperties = Class.forName("android.os.SystemProperties");

  178.  
  179.           //参数类型

  180.           @SuppressWarnings("rawtypes")

  181.               Class[] paramTypes= new Class[2];

  182.           paramTypes[0]= String.class;

  183.           paramTypes[1]= String.class;  

  184.           Method set = SystemProperties.getMethod("set", paramTypes);

  185.           //参数     

  186.           Object[] params= new Object[2];

  187.           params[0]= new String(key);

  188.           params[1]= new String(val);

  189.           set.invoke(SystemProperties, params);

  190.         }catch( IllegalArgumentException iAE ){

  191.             throw iAE;

  192.         }catch( Exception e ){

  193.             //TODO

  194.         }

  195.     }

  196. }

这是第一种方法, 这也是一种万能的方法.

当然如果不是非常必要的话, 建议还是不要使用这样的万能方法.

第二种方法是其它如果只是要简单的判断是否为魅族手机的话, 完全可以调用google提供的api来做.

android.os.Build.BRAND

使用上面的属性如果是魅族手机的话返回返回Meizu.

这是和我使用adb shell prop看到的属性是一致的, 可以看下图

看到该类的源码也可以看到如下内容

  1. public class Build {

  2.     /** Value used for when a build property is unknown. */

  3.     public static final String UNKNOWN = "unknown";

  4.     /** Either a changelist number, or a label like "M4-rc20". */

  5.     public static final String ID = getString("ro.build.id");

  6.     /** A build ID string meant for displaying to the user */

  7.     public static final String DISPLAY = getString("ro.build.display.id");

  8.     /** The name of the overall product. */

  9.     public static final String PRODUCT = getString("ro.product.name");

  10.     /** The name of the industrial design. */

  11.     public static final String DEVICE = getString("ro.product.device");

  12.     /** The name of the underlying board, like "goldfish". */

  13.     public static final String BOARD = getString("ro.product.board");

  14.     /** The name of the instruction set (CPU type + ABI convention) of native code. */

  15.     public static final String CPU_ABI = getString("ro.product.cpu.abi");

  16.     /** The name of the second instruction set (CPU type + ABI convention) of native code. */

  17.     public static final String CPU_ABI2 = getString("ro.product.cpu.abi2");

  18.     /** The manufacturer of the product/hardware. */

  19.     public static final String MANUFACTURER = getString("ro.product.manufacturer");

  20.     /** The brand (e.g., carrier) the software is customized for, if any. */

  21.     public static final String BRAND = getString("ro.product.brand");

  22.     /** The end-user-visible name for the end product. */

  23.     public static final String MODEL = getString("ro.product.model");

  24.     /** The system bootloader version number. */

  25.     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获取系统属性相关推荐

  1. Android 9无法通过反射获取系统属性

    Android 9.0之后获取无法通过反射获取系统属性,如果我们需要知道手机某个属性,可以通过以下的方式.注意:这种方式不能获取所有的属性.不然谷歌限制SystemProper的访问就没有意义了. p ...

  2. Android反射set/get系统属性(SystemProperties)

    以Android系统版本号和SDK版本号为例 # setprop ro.build.version.sdk 23 # setprop ro.build.version.release 5.1.1 fr ...

  3. java获取系统属性_Java获取系统属性

    1.System类 The System class contains several useful class fields and methods. It cannot be instantiat ...

  4. 在Java中获取系统属性

    Java语言以其面向对象.跨平台.可移植性好.安全性高等优点,受到众多编程人员的青睐,越来越多的人将其作为应用软件开发语言的首选. 在Java应用程序运行时,特别是需要在跨平台工作环境下运行时,需要确 ...

  5. android第三方app改为系统app,加入system组,获取system权限

    用Androd studio 开发的app,编译出apk, 想获取system权限. 环境:编译好的apk, android 源码环境,有root权限和源码对应的开发板,我这里是user版本. 思路: ...

  6. android内置第三方APP为系统应用,第三方APP内置到/system/app目录下报错java.lang.UnsatisfiedLinkError,so文件不加载

    项目场景: 在无系统签名的情况下,将第三方APP内置到/system/app目录下,可以将第三方应用变为系统应用,用户无法直接卸载.不过前提是手机需要root. 问题描述 使用es文件管理工具将第三方 ...

  7. Android adb 设置和获取系统属性

    /system/build.prop 为系统属性默认值. 安卓系统属性由特殊的property_service管理, /system/build.prop是只读文件, 其中包含property_ser ...

  8. Android SystemProperties设置/取得系统属性的用法总结

    2019独角兽企业重金招聘Python工程师标准>>> 1.Android SystemProperties简介 介绍了设置属性需要的权限,已经设置权限的方法. Systemprop ...

  9. Android第三方app 微信授权登录

    首先呢,说到第三方,大家应该首先想到的是: 1.appid (第三方给的) 2.密钥 (第三方给的) 3.签名文件 4.包名 这4样,往往决定着的是第三方好用不好用!!! 所以呢- 1.申请你的App ...

最新文章

  1. python 利用jieba读取txt文本进行分词后存入新txt
  2. 2021 AAAI Fellow名单重磅出炉,华人学者遗憾连续两年无缘入选 | AI日报
  3. linux命令之上传文件和下载文件
  4. TCP/IP 原理--链路层
  5. AWS 专家教你快速使用 Spring Boot 和 DJL!
  6. Vue项目webpack打包部署到Tomcat,刷新报404错
  7. 50. 模型层 --- dao 层(2)
  8. centos6安装mysql并远程连接_Linux中Mysql不支持远程连接解决办法
  9. C语言程序设计答何钦铭答案,c语言程序设计何钦铭课后题答案
  10. 关于STM32xE系列芯片STOP模式下使用RTC唤醒所遇到的问题记录
  11. Mac使用——MongoDB的下载和Compass可视化工具,以及安装过程
  12. c语言中提取单词首字母,C语言实现单词小帮手
  13. 睡眠时的局部目标记忆再激活
  14. 问:平面布置图是什么?有什么作用?如何判断好的平面布置图方案?如何绘制?
  15. 【第一期】大话计算机网络(猫、路由器、wifi)
  16. 社保随便挂靠,会判刑罚款您知道吗?
  17. 学习用Photoshop来设计简单的网页
  18. 计算机网络钟中PCF,请问,为什么我向时钟芯片PCF8563中写入初始化数据后,读出来...
  19. 团队开发——极速蜗牛
  20. 超诡异的自杀案,有点坑爹

热门文章

  1. 飞腾交叉编译环境搭建之交叉编译工具链配置
  2. 期货方法(期货方法很简单 只用MACD)
  3. 【附源码】Java计算机毕业设计校友社交系统(程序+LW+部署)
  4. 2018-2019赛季多校联合新生训练赛第六场补题与题解(中石油)
  5. 小站长如何利用软文进行推广网站
  6. VB用API模拟截屏键PrintScreen
  7. 原装苹果手机_二手原装正品苹果手机及平板批发报价单353
  8. 马尔科夫系列——三、隐马尔可夫模型 - 学习问题 - Baum-Welch算法
  9. 三种计算开关电源控制器结温的方法
  10. Chrome浏览器扩展开发之自动化操作页面