android.os.Build 详解

本文深入解析了Android os Build类的属性及其获取方法,包括如何通过反射读取build.prop文件中的属性值,如何获取指纹分区、射频固件版本、设备标识符等信息,并介绍了获取Android版本号、设备型号、制造商等关键设备信息的方法。

android.os.Build 详解

前言

本文地址 https://blog.youkuaiyun.com/CSqingchen/article/details/51304429
最新 文章连接,本文不再同步


常见函数及其值

val build = "Build.BOARD:${Build.BOARD}\n" +
        "Build.BOOTLOADER:${Build.BOOTLOADER}\n" +
        "Build.BRAND:${Build.BRAND}\n" +
        "Build.DEVICE:${Build.DEVICE}\n" +
        "Build.DISPLAY:${Build.DISPLAY}\n" +
        "Build.FINGERPRINT:${Build.FINGERPRINT}\n" +
        "Build.HARDWARE:${Build.HARDWARE}\n" +
        "Build.HOST:${Build.HOST}\n" +
        "Build.ID:${Build.ID}\n" +
        "Build.MANUFACTURER:${Build.MANUFACTURER}\n" +
        "Build.MODEL:${Build.MODEL}\n" +
        "Build.PRODUCT:${Build.PRODUCT}\n" +
        "Build.TIME:${Build.TIME}\n" +
        "Build.TYPE:${Build.TYPE}\n" +
        "Build.USER:${Build.USER}\n" +
        "Build.Partition.PARTITION_NAME_SYSTEM:${Build.Partition.PARTITION_NAME_SYSTEM}\n" +
        "Build.VERSION.BASE_OS:${Build.VERSION.BASE_OS}\n" +
        "Build.VERSION.CODENAME:${Build.VERSION.CODENAME}\n" +
        "Build.VERSION.INCREMENTAL:${Build.VERSION.INCREMENTAL}\n" +
        "Build.VERSION.PREVIEW_SDK_INT:${Build.VERSION.PREVIEW_SDK_INT}\n" +
        "Build.VERSION.RELEASE:${Build.VERSION.RELEASE}\n" +
        "Build.VERSION.SDK_INT:${Build.VERSION.SDK_INT}\n" +
        "Build.VERSION.SECURITY_PATCH:${Build.VERSION.SECURITY_PATCH}\n"

结果如下:

Build.BOARD:sp9863a_1h10
Build.BOOTLOADER:unknown
Build.BRAND:Teclast
Build.DEVICE:p20_n6h1
Build.DISPLAY:V1.06_20210406
Build.FINGERPRINT:rockchip/rk312x/rk312x:6.0.1/MXC89K/user.fyl.20170927.202757:userdebug/test-keys
Build.HARDWARE:s9863a1h10
Build.HOST:android-build
Build.ID:QP1A.190711.020
Build.MANUFACTURER:Teclast
Build.MODEL:P20HD(N6H1)
Build.PRODUCT:p20_n6h1
Build.TIME:1617700065000
Build.TYPE:user
Build.USER:yhg229
Build.Partition.PARTITION_NAME_SYSTEM:system
Build.VERSION.BASE_OS:
Build.VERSION.CODENAME:REL
Build.VERSION.INCREMENTAL:14217
Build.VERSION.PREVIEW_SDK_INT:0
Build.VERSION.RELEASE:10
Build.VERSION.SDK_INT:29
Build.VERSION.SECURITY_PATCH:2020-04-05

这些信息包含设备的厂商及Android版本相关信息


adb 读取 build.prop 中内容

在早期安卓版本和已经ROOT的系统,使用如下命令,可以在终端查看/system/build.prop中所有的key和value
adb shell "cat /system/build.prop"
新的安卓系统从安全上考虑,已经加了权限限制,无法读取,除非ROOT,如下图

有些人说可以通过读取文件获取其中的值,目前也是不行的,会提示 Permission denied ,原因同上。


通过反射读取 build.prop 中 key对应的value

使用以下代码可以在代码中,获取 /system/build.prop 中key对应的value

private String getBuildPropValue(String key) {
    String value = null;
    try {
        Method method = Build.class.getDeclaredMethod("getString", String.class);
        method.setAccessible(true);
        value = (String) method.invoke(new Build(), key);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return value;
}

使用方法如下
getBuildPropValue("persist.sys.timezone")


Build.getFingerprintedPartitions()

这是一个API 29(Android10)加入的API,参考自 getFingerprintedPartitions
得到的List<Build.Partition>数据类似如下

partition[name:bootimage,fingerprint:rockchip/rk312x/rk312x:6.0.1/MXC89K/user.fyl.20170927.202757:userdebug/test-keys,buildTimeMillis:1617700065000]
partition[name:odm,fingerprint:rockchip/rk312x/rk312x:6.0.1/MXC89K/user.fyl.20170927.202757:userdebug/test-keys,buildTimeMillis:1617699225000]
partition[name:product,fingerprint:rockchip/rk312x/rk312x:6.0.1/MXC89K/user.fyl.20170927.202757:userdebug/test-keys,buildTimeMillis:1617699225000]
partition[name:system,fingerprint:rockchip/rk312x/rk312x:6.0.1/MXC89K/user.fyl.20170927.202757:userdebug/test-keys,buildTimeMillis:1617700065000]
partition[name:vendor,fingerprint:rockchip/rk312x/rk312x:6.0.1/MXC89K/user.fyl.20170927.202757:userdebug/test-keys,buildTimeMillis:1617700065000]

Build.getRadioVersion()

返回的是 射频固件版本信息,结果类似如下
FM_BASE_18B_W20.08.3|sc9863A_modem|02-19-2020 01:21:39

本文地址 https://blog.youkuaiyun.com/CSqingchen/article/details/51304429


Build.getSerial() 设备标识符

这个函数是其中最复杂的一个,在API 26(Android 8.0)加入,返回硬件标识符。

  • 在Android 8.0 中需要在 AndroidManifest.xml 添加以下权限
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    而且需要动态申请权限
    PackageManager.PERMISSION_GRANTED != activity?.checkSelfPermission(Manifest.permission.READ_PHONE_STATE))
    ActivityCompat.requestPermissions(,,)
    返回结果类似 7c422495,跟adb devices列出的设备标识符一致
    在这里插入图片描述

  • 在Android 9.0 以后,持久设备标识符在附加限制之后被保护,需要权限 READ_PRIVILEGED_PHONE_STATE,
    在源码 frameworks/core/res/AndroidManifest.xml 中可以看到如下

    <!-- @SystemApi @TestApi Allows read access to privileged phone state.
         @hide Used internally. -->
    <permission android:name="android.permission.READ_PRIVILEGED_PHONE_STATE"
        android:protectionLevel="signature|privileged" />
    

    这个权限只有在privileged应用(apk内置至system/priv-app目录)才可以访问,即第三方应用无法访问。
    Android 10 开始,同样受影响的还有TelephonyManager的函数getImei()、getDeviceId()、getMeid()、getSimSerialNumber()、getSubscriberId(),参见 文档 device-identifiers

  • 总结,对于第三方应用,只有在安卓8.0设备才能获取到对应的值,其它情况要么返回 Build#UNKNOWN,要么抛出异常SecurityException,要么抛出未授权异常。

  • 要解决SecurityException 异常 does not meet the requirements to access device identifiers,就需要重新设计获取设备标识相关的代码,可以参考
    https://developer.android.com/training/articles/user-data-ids

参考自 https://developer.android.com/reference/android/os/Build#getSerial()


参考自:
http://www.cnblogs.com/onlywujun/p/3519037.html
java method invoke private
ANDROID_ID(SSAID)设备标识

<think>我们正在解决一个Android开发中的问题:应用启动前台服务时,因无效通知导致`RemoteServiceException`异常。具体错误信息是`Bad notification for startForeground`,常见原因包括通知渠道无效、通知图标未设置或为零等。 根据引用[1][2][3]中的错误信息,我们可以总结出以下常见原因: 1. 通知渠道(Notification Channel)无效(如未创建或渠道ID不匹配)[^1][^3] 2. 通知图标(icon)未设置或资源ID为零(即未找到有效的图标资源)[^2] 3. 其他通知配置错误(如未设置必要属性) ### 解决方案步骤: #### 1. **确保已创建通知渠道(Android 8.0+)** - 在创建通知前,必须为应用创建通知渠道(仅需一次) - 示例代码: ```java public void createNotificationChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { String channelId = "my_channel_id"; // 必须与通知使用的channelId一致 String channelName = "My Channel"; int importance = NotificationManager.IMPORTANCE_DEFAULT; NotificationChannel channel = new NotificationChannel(channelId, channelName, importance); NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } } ``` - 在应用启动时(如`Application`的`onCreate`或服务启动前)调用此方法[^1][^3] #### 2. **确保通知使用有效的图标** - 通知必须设置一个非零的图标资源(通常为mipmap中的图标) - 在`NotificationCompat.Builder`中必须调用`setSmallIcon()`并传递有效资源ID - 示例: ```java Notification notification = new NotificationCompat.Builder(this, "my_channel_id") .setSmallIcon(R.drawable.ic_notification) // 必须使用有效的图标资源 .setContentTitle("服务运行中") .setContentText("正在执行后台任务") .build(); ``` #### 3. **正确构建通知并启动前台服务** - 完整的前台服务通知构建示例: ```java public class MyService extends Service { private static final String CHANNEL_ID = "my_channel_id"; @Override public void onCreate() { super.onCreate(); createNotificationChannel(); // 确保渠道已创建 } @Override public int onStartCommand(Intent intent, int flags, int startId) { // 构建通知 Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.mipmap.ic_launcher) // 替换为你的图标资源 .setContentTitle("服务运行中") .setContentText("正在执行后台任务") .build(); // 启动前台服务(API级别>=5) startForeground(1, notification); // 通知ID必须非零 return START_STICKY; } } ``` #### 4. **检查资源文件是否存在** - 确保`setSmallIcon`使用的图标资源存在于`res/mipmap`或`res/drawable`目录 - 避免使用透明图标(某些系统版本会视为无效) #### 5. **适配Android 12(API 31)及以上** -Android 12开始,前台服务通知必须包含有效的`contentIntent`(即点击通知的PendingIntent) - 添加`setContentIntent`: ```java // 创建一个点击通知时打开的Intent Intent notificationIntent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE); Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("服务运行中") .setContentText("正在执行后台任务") .setContentIntent(pendingIntent) // 必须设置 .build(); ``` ### 错误排查清单: - [ ] 是否在Android 8.0+设备上测试? - [ ] 是否在调用`startForeground`前创建了通知渠道? - [ ] 通知构建器中设置的`channelId`是否与创建的渠道ID一致? - [ ] 是否调用了`setSmallIcon`并传递了有效的资源ID(非零)? - [ ] 在Android 12+设备上,是否设置了`contentIntent`? - [ ] 通知ID是否非零? > **注意**:如果问题仍然存在,尝试清理项目并重启Android Studio(引用[4]中提到缓存可能导致奇怪问题)[^4]。 §§相关问题§§ 1. 如何为Android前台服务创建持续通知? 2. Android 12前台服务启动限制详解 3. 解决NotificationChannel兼容性问题 4. Android服务保活机制与系统限制</think>### 解决 Android 前台服务启动时 `RemoteServiceException: Bad notification` 异常 当 Android 应用启动前台服务时,如果通知配置无效,系统会抛出 `RemoteServiceException: Bad notification for startForeground` 异常。以下是完整解决方案: #### 一、核心错误原因分析(根据引用内容) 1. **无效的通知渠道**(最常见原因)[^1][^3] ```java // 错误示例:未创建通知渠道直接使用 Notification(channel=null ...) // 导致 invalid channel 错误 ``` 2. **通知图标未设置或无效**(必须是非零资源)[^2] ```java java.lang.RuntimeException: icon must be non-zero ``` 3. **Android 8.0+ 未适配通知渠道机制** Android 8.0 (API 26) 起强制要求使用通知渠道 --- #### 二、完整解决方案步骤 ##### 1. 创建有效的通知渠道(Android 8.0+) ```java private void createNotificationChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { String channelId = "foreground_service_channel"; String channelName = "前台服务通知"; NotificationChannel channel = new NotificationChannel( channelId, channelName, NotificationManager.IMPORTANCE_LOW // 重要级别 ); NotificationManager manager = getSystemService(NotificationManager.class); manager.createNotificationChannel(channel); } } // 在 Application 或 Service 的 onCreate() 中调用 ``` ##### 2. 构建符合要求的通知 ```java private Notification buildNotification() { String channelId = "foreground_service_channel"; // 必须设置有效的小图标(非零资源) int iconRes = R.drawable.ic_service_notification; // 在 res/drawable 存在的资源 PendingIntent pendingIntent = ... // 必须设置有效的点击意图(Android 12+要求) return new NotificationCompat.Builder(this, channelId) .setSmallIcon(iconRes) // 必须设置且非零[^2] .setContentTitle("服务运行中") .setContentText("正在执行后台任务") .setContentIntent(pendingIntent) // Android 12+ 强制要求 .setPriority(NotificationCompat.PRIORITY_LOW) .build(); } ``` ##### 3. 正确启动前台服务 ```java public class MyForegroundService extends Service { private static final int NOTIFICATION_ID = 1001; @Override public void onCreate() { super.onCreate(); createNotificationChannel(); // 确保渠道已创建 } @Override public int onStartCommand(Intent intent, int flags, int startId) { Notification notification = buildNotification(); // 启动前台服务(API 级别 >= 5) startForeground(NOTIFICATION_ID, notification); return START_STICKY; } } ``` --- #### 三、各 Android 版本适配要点 | **Android 版本** | **关键要求** | **适配代码示例** | |------------------|---------------------------------------|--------------------------------------------------| | **< 8.0 (Oreo)** | 不需要通知渠道 | 跳过渠道创建步骤 | | **8.0-11 (O)** | 必须创建通知渠道 | 使用 `NotificationChannel` | | **12+ (S)** | 必须设置 `contentIntent` | `.setContentIntent(validPendingIntent)` | | **13+ (T)** | 需要 `POST_NOTIFICATIONS` 权限 | 添加 `<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>` | --- #### 四、错误排查清单 1. ✅ 检查 `setSmallIcon()` 使用的资源是否存在且非透明 2. ✅ 确认通知渠道 ID 与构建器使用的 ID 完全一致[^3] 3.Android 12+ 必须设置 `setContentIntent()` 4. ✅ 确保在 `startForeground()` 前创建通知渠道 5. ✅ 使用 `NotificationCompat.Builder` 保证向后兼容 6. ✅ 清除应用缓存后测试(IDE 缓存可能导致异常)[^4] > **关键提示**:在 Android 10+ 设备上,可通过以下命令检查通知配置: > `adb shell dumpsys notification | grep "Foreground service"` --- #### 五、完整示例代码 ```java // ForegroundService.java public class ForegroundService extends Service { @Override public void onCreate() { super.onCreate(); createNotificationChannel(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Notification notification = new NotificationCompat.Builder(this, "foreground_service_channel") .setSmallIcon(R.drawable.ic_service_icon) // 必须使用有效资源 .setContentTitle("后台服务运行中") .setContentText("正在执行重要任务") .setContentIntent(getPendingIntent()) // Android 12+ 必需 .build(); startForeground(1001, notification); return START_STICKY; } private PendingIntent getPendingIntent() { Intent intent = new Intent(this, MainActivity.class); return PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE); } private void createNotificationChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel( "foreground_service_channel", "服务通知", NotificationManager.IMPORTANCE_LOW ); NotificationManager manager = getSystemService(NotificationManager.class); manager.createNotificationChannel(channel); } } @Nullable @Override public IBinder onBind(Intent intent) { return null; } } ```
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

清霜辰

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

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

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

打赏作者

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

抵扣说明:

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

余额充值