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

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

 
  1. package me.chasel.utils

  2.  
  3. import java.io.File;

  4. import java.lang.reflect.Method;

  5. import android.content.Context;

  6. import dalvik.system.DexFile;

  7.  
  8.  
  9. public class SystemPropertiesProxy

  10. {

  11.  
  12.     /**

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

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

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

  16.      */

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

  18.  
  19.         String ret= "";

  20.  
  21.         try{

  22.  
  23.           ClassLoader cl = context.getClassLoader(); 

  24.           @SuppressWarnings("rawtypes")

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

  26.  
  27.           //参数类型

  28.           @SuppressWarnings("rawtypes")

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

  30.           paramTypes[0]= String.class;

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

  33.  
  34.           //参数

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

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

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

  39.  
  40.         }catch( IllegalArgumentException iAE ){

  41.             throw iAE;

  42.         }catch( Exception e ){

  43.             ret= "";

  44.             //TODO

  45.         }

  46.  
  47.         return ret;

  48.  
  49.     }

  50.  
  51.     /**

  52.      * 根据Key获取值.

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

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

  55.      */

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

  57.  
  58.         String ret= def;

  59.  
  60.         try{

  61.  
  62.           ClassLoader cl = context.getClassLoader(); 

  63.           @SuppressWarnings("rawtypes")

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

  65.  
  66.           //参数类型

  67.           @SuppressWarnings("rawtypes")

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

  69.           paramTypes[0]= String.class;

  70.           paramTypes[1]= String.class;          

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

  73.  
  74.           //参数

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

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

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

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

  80.  
  81.         }catch( IllegalArgumentException iAE ){

  82.             throw iAE;

  83.         }catch( Exception e ){

  84.             ret= def;

  85.             //TODO

  86.         }

  87.  
  88.         return ret;

  89.  
  90.     }

  91.  
  92.     /**

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

  94.      * @param key 要查询的key

  95.      * @param def 默认返回值

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

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

  98.      */

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

  100.  
  101.         Integer ret= def;

  102.  
  103.         try{

  104.  
  105.           ClassLoader cl = context.getClassLoader(); 

  106.           @SuppressWarnings("rawtypes")

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

  108.  
  109.           //参数类型

  110.           @SuppressWarnings("rawtypes")

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

  112.           paramTypes[0]= String.class;

  113.           paramTypes[1]= int.class;  

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

  116.  
  117.           //参数

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

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

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

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

  123.  
  124.         }catch( IllegalArgumentException iAE ){

  125.             throw iAE;

  126.         }catch( Exception e ){

  127.             ret= def;

  128.             //TODO

  129.         }

  130.  
  131.         return ret;

  132.  
  133.     }

  134.  
  135.     /**

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

  137.      * @param key 要查询的key

  138.      * @param def 默认返回值

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

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

  141.      */

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

  143.  
  144.         Long ret= def;

  145.  
  146.         try{

  147.  
  148.           ClassLoader cl = context.getClassLoader();

  149.           @SuppressWarnings("rawtypes")

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

  151.  
  152.           //参数类型

  153.           @SuppressWarnings("rawtypes")

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

  155.           paramTypes[0]= String.class;

  156.           paramTypes[1]= long.class;  

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

  159.  
  160.           //参数

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

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

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

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

  166.  
  167.         }catch( IllegalArgumentException iAE ){

  168.             throw iAE;

  169.         }catch( Exception e ){

  170.             ret= def;

  171.             //TODO

  172.         }

  173.  
  174.         return ret;

  175.  
  176.     }

  177.  
  178.     /**

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

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

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

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

  183.      * @param key 要查询的key

  184.      * @param def 默认返回值

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

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

  187.      */

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

  189.  
  190.         Boolean ret= def;

  191.  
  192.         try{

  193.  
  194.           ClassLoader cl = context.getClassLoader(); 

  195.           @SuppressWarnings("rawtypes")

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

  197.  
  198.           //参数类型

  199.           @SuppressWarnings("rawtypes")

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

  201.           paramTypes[0]= String.class;

  202.           paramTypes[1]= boolean.class;  

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

  205.  
  206.           //参数     

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

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

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

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

  212.  
  213.         }catch( IllegalArgumentException iAE ){

  214.             throw iAE;

  215.         }catch( Exception e ){

  216.             ret= def;

  217.             //TODO

  218.         }

  219.  
  220.         return ret;

  221.  
  222.     }

  223.  
  224.     /**

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

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

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

  228.      */

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

  230.  
  231.         try{

  232.  
  233.           @SuppressWarnings("unused")

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

  235.           @SuppressWarnings("unused")

  236.           ClassLoader cl = context.getClassLoader(); 

  237.           @SuppressWarnings("rawtypes")

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

  239.  
  240.           //参数类型

  241.           @SuppressWarnings("rawtypes")

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

  243.           paramTypes[0]= String.class;

  244.           paramTypes[1]= String.class;  

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

  247.  
  248.           //参数     

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

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

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

  252.  
  253.           set.invoke(SystemProperties, params);

  254.  
  255.         }catch( IllegalArgumentException iAE ){

  256.             throw iAE;

  257.         }catch( Exception e ){

  258.             //TODO

  259.         }

  260.  
  261.     }

  262. }

 

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

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

第二种方法是其它如果只是要简单的判断是否为魅族手机的话, 完全可以调用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.  
  5.     /** Either a changelist number, or a label like "M4-rc20". */

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

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

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

  10.  
  11.     /** The name of the overall product. */

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

  13.  
  14.     /** The name of the industrial design. */

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

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

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

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

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

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

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

  25.  
  26.     /** The manufacturer of the product/hardware. */

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

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

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

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

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

  34.  
  35.     /** The system bootloader version number. */

  36.     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;获取设备名字

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值