Android应用开发(32)获取Property系统属性

文章介绍了在Android应用开发中获取系统属性的三种方式:通过android.os.Build类获取有限的属性信息,使用android.os.SystemPropertiesAPI(仅限系统应用),以及第三方应用通过反射调用SystemProperties获取属性。示例代码展示了如何在实践中运用这些方法。

Android应用开发学习笔记——目录索引

开发过程中我们经常需要获取Property系统属性,比如model、device、product 等等,通常debug的时候可以使用如下命令获取。

adb shell getprop <property name>
adb shell setprop <property name> <property value>

但是一个android 应用程序中需要获取系统的属性,该怎么获取呢?

通常有就如下几种方法:

  1. 使用android.os.Build提供的API,所有应用皆可使用,但是能获取的property非常有限
  2. 使用android.os.SystemProperties提供的API,但仅系统app可用
  3. 使用反射机制获取property,所有第三方app都可以使用

一、使用android.os.Build提供的API

frameworks/base/core/java/android/os/Build.java

public class Build {
    private static final String TAG = "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 manufacturer of the product/hardware. */
    public static final String MANUFACTURER = getString("ro.product.manufacturer");

    /** The consumer-visible brand with which the product/hardware will be associated, 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 manufacturer of the device's primary system-on-chip. */
    @NonNull
    public static final String SOC_MANUFACTURER = SocProperties.soc_manufacturer().orElse(UNKNOWN);

    /** The model name of the device's primary system-on-chip. */
    @NonNull
    public static final String SOC_MODEL = SocProperties.soc_model().orElse(UNKNOWN);

    /** The system bootloader version number. */
    public static final String BOOTLOADER = getString("ro.bootloader");
...
    /** Various version strings. */
    public static class VERSION {
        /**
         * The internal value used by the underlying source control to
         * represent this build.  E.g., a perforce changelist number
         * or a git hash.
         */
        public static final String INCREMENTAL = getString("ro.build.version.incremental");

        /**
         * The user-visible version string.  E.g., "1.0" or "3.4b5" or "bananas".
         *
         * This field is an opaque string. Do not assume that its value
         * has any particular structure or that values of RELEASE from
         * different releases can be somehow ordered.
         */
        public static final String RELEASE = getString("ro.build.version.release");
...
        public static final String SDK = getString("ro.build.version.sdk");
        public static final int SDK_INT = SystemProperties.getInt(
                "ro.build.version.sdk", 0);
...
    }

    @UnsupportedAppUsage
    private static String getString(String property) {
        return SystemProperties.get(property, UNKNOWN);
    }

    private static String[] getStringList(String property, String separator) {
        String value = SystemProperties.get(property);
        if (value.isEmpty()) {
            return new String[0];
        } else {
            return value.split(separator);
        }
    }

    @UnsupportedAppUsage
    private static long getLong(String property) {
        try {
            return Long.parseLong(SystemProperties.get(property));
        } catch (NumberFormatException e) {
            return -1;
        }
    }

API在应用程序中使用非常简单:

// 1. import
import android.os.Build;

// 2. 直接使用
Log.d(TAG, "Build.PRODUCT: " + Build.PRODUCT);
Log.d(TAG, "Build.MODEL: " + Build.MODEL);
Log.d(TAG, "Build.SOC_MODEL: " + Build.SOC_MODEL);
Log.d(TAG, "Build.MANUFACTURER: " + Build.MANUFACTURER);

// Android 4.4W (KitKat Wear)系统及以上
if (Build.VERSION.SDK_INT >= 20) {
    do something
} else {
    do something
}

如我使用xiaomi 13 Ultra,打印如下:

D/lzl-test: Build.PRODUCT: ishtar
D/lzl-test: Build.MODEL: 2304FPN6DC
D/lzl-test: Build.SOC_MODEL: SM8550
D/lzl-test: Build.MANUFACTURER: Xiaomi 

 二、使用android.os.SystemProperties提供的API

frameworks/base/core/java/android/os/SystemProperties.java

public class SystemProperties {
    private static final String TAG = "SystemProperties";

    @NonNull
    @SystemApi
    public static String get(@NonNull String key, @Nullable String def) {
        if (TRACK_KEY_ACCESS) onKeyAccess(key);
        return native_get(key, def);
    }

    @SystemApi
    public static int getInt(@NonNull String key, int def) {
        if (TRACK_KEY_ACCESS) onKeyAccess(key);
        return native_get_int(key, def);
    }

    @SystemApi
    public static boolean getBoolean(@NonNull String key, boolean def) {
        if (TRACK_KEY_ACCESS) onKeyAccess(key);
        return native_get_boolean(key, def);
    }
...

  使用非常简单,但API只有系统应用才有权限,第三方应用请使用第三种方法。

三、使用反射机制获取property

    public static String getProperty(String key, String defaultValue) {
        String value = defaultValue;
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method get = c.getMethod("get", String.class, String.class);
            value = (String) (get.invoke(c, key, "unknown"));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return value;
        }
    }

    public static int setProperty(String key, String value) {
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method set = c.getMethod("set", String.class, String.class);
            set.invoke(c, key, value);
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

四、测试程序

 源码

百度网盘链接:百度网盘 请输入提取码 提取码:test

github下载地址:

GitHub - liuzhengliang1102/AndroidStudio-LearnAppDevelopment

PropertyTest目录

运行

左边:使用xiaomi 13 Ultra运行截图,右边:使用Pixel 6 API 33 模拟器运行截图

        

  

点此查看Android应用开发学习笔记的完整目录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liuzl_2010

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值