获取Android唯一设备号

本文介绍了一个Java类,用于获取Android系统的属性值,包括长整型、布尔型及字符串类型的数据。通过反射机制实现了对系统属性的读取,并提供了错误处理机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class SystemPropertiesInvoke {
private static final String TAG = “SystemPropertiesInvoke”;
private static Method getLongMethod = null;
private static Method getBooleanMethod = null;

public static long getLong(final String key, final long def) {
    try {
        if (getLongMethod == null) {
            getLongMethod = Class.forName("android.os.SystemProperties")
                    .getMethod("getLong", String.class, long.class);
        }

        return ((Long) getLongMethod.invoke(null, key, def)).longValue();
    } catch (Exception e) {
        Log.e(TAG, "Platform error: " + e.toString());
        return def;
    }
}

public static boolean getBoolean(final String key, final boolean def) {
    try {
        if (getBooleanMethod == null) {
            getBooleanMethod = Class.forName("android.os.SystemProperties")
                    .getMethod("getBoolean", String.class,boolean.class);
        }

return (Boolean)getBooleanMethod.invoke(null, key, def);
} catch (Exception e) {
Log.e(TAG, “Platform error: ” + e.toString());
return def;
}
}

public static String getString(final String key, final String def) {
    try {
        if (getBooleanMethod == null) {
            getBooleanMethod = Class.forName("android.os.SystemProperties")
                    .getMethod("getString", String.class,boolean.class);
        }
return (String)getBooleanMethod.invoke(null, key, def);
    } catch (Exception e) {
        Log.e(TAG, "Platform error: " + e.toString());
        return def;
    }
}

}

调用方式
String serial = SystemPropertiesInvoke.getString(“gsm.serial”, “”);
Log.i(“TAG”, “serial==========” + serial);
请求权限




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值