Android應用獲取系統屬性

使用adb shell prop能夠查看Android系統的屬性.詳情看下圖java

上面列出了不少屬性, 若是要在使用中使用該屬性, 好比說我是爲了判斷該手機是否是魅族手機.android

google在framework層提供了一個SystemProperties類, 該類位於框架層, 具體位置是frameworks/base/core/java/android/os/SystemProperties.javashell

該類提供了很詳細的訪問各類屬性的方法,因此我可使用反射機制來獲取我想要的參數.api

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
        }

    }
}

這是第一種方法, 這也是一種萬能的方法.app

固然若是不是很是必要的話, 建議仍是不要使用這樣的萬能方法.框架

第二種方法是其它若是隻是要簡單的判斷是否爲魅族手機的話, 徹底能夠調用google提供的api來作.ui

android.os.Build.BRAND

使用上面的屬性若是是魅族手機的話返回返回Meizu.google

這是和我使用adb shell prop看到的屬性是一致的, 能夠看下圖code

看到該類的源碼也能夠看到以下內容字符串

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");


經過以上兩種方法基本上能夠解決掉該問題了.

相關文章
相關標籤/搜索