Android 12(API 31)引入了多项重要特性和行为变更,本文从开发视角梳理核心适配点,结合 Kotlin 代码示例说明具体实现方式。
一、核心适配前提
- 升级开发环境:
- Gradle 插件 ≥ 7.0.0
- compileSdkVersion ≥ 31
- targetSdkVersion 建议升级到 31(若暂不升级,部分特性仍需适配)
- 依赖库升级:确保 AppCompat、Material 等库版本 ≥ 1.4.0
二、关键特性适配
1. 前台服务(Foreground Service)限制
Android 12 收紧了前台服务权限,仅允许特定场景使用,且新增POST_NOTIFICATIONS权限。
适配要点:
- 声明前台服务类型(必须在 Manifest 中指定)
- 动态申请通知权限(Android 13+ 强制,12 建议提前适配)
- 非允许场景使用 WorkManager 替代
代码示例:
1. Manifest 配置
<!-- 声明前台服务类型 --><serviceandroid:name=".MyForegroundService"android:foregroundServiceType="location|mediaPlayback|phoneCall"/><!-- 声明通知权限 --><uses-permissionandroid:name="android.permission.POST_NOTIFICATIONS"/>2. 动态申请通知权限(Kotlin)
// 检查并申请通知权限privatefunrequestNotificationPermission(){if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.TIRAMISU){if(ContextCompat.checkSelfPermission(this,Manifest.permission.POST_NOTIFICATIONS)!=PackageManager.PERMISSION_GRANTED){ActivityResultContracts.RequestPermission()registerForActivityResult(ActivityResultContracts.RequestPermission()){isGranted->if(isGranted){// 权限已授予,启动前台服务startForegroundService()}else{// 引导用户手动开启权限showPermissionDialog()}}.launch(Manifest.permission.POST_NOTIFICATIONS)}else{startForegroundService()}}else{// Android 12 及以下直接启动startForegroundService()}}// 启动前台服务privatefunstartForegroundService(){valserviceIntent=Intent(this,MyForegroundService::class.java)if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){startForegroundService(serviceIntent)}else{startService(serviceIntent)}}3. 前台服务实现(Kotlin)
classMyForegroundService:Service(){privatevalNOTIFICATION_ID=1001privatevalCHANNEL_ID="FOREGROUND_CHANNEL"overridefunonStartCommand(intent:Intent?,flags:Int,startId:Int):Int{createNotificationChannel()valnotification=NotificationCompat.Builder(this,CHANNEL_ID).setContentTitle("前台服务").setContentText("服务运行中").setSmallIcon(R.mipmap.ic_launcher).build()// Android 12 必须指定服务类型if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.S){startForeground(NOTIFICATION_ID,notification,ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION// 匹配Manifest声明的类型)}else{startForeground(NOTIFICATION_ID,notification)}// 业务逻辑...returnSTART_STICKY}privatefuncreateNotificationChannel(){if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){valchannel=NotificationChannel(CHANNEL_ID,"前台服务通道",NotificationManager.IMPORTANCE_LOW)valmanager=getSystemService(NotificationManager::class.java)manager.createNotificationChannel(channel)}}overridefunonBind(intent:Intent?):IBinder?=null}2. 精确闹钟权限(SCHEDULE_EXACT_ALARM)
Android 12 要求使用精确闹钟(setExact()/setExactAndAllowWhileIdle())必须申请SCHEDULE_EXACT_ALARM权限。
适配要点:
- Manifest 声明权限
- 运行时检查并申请权限
- 处理权限被拒绝的场景
代码示例:
1. Manifest 配置
<uses-permissionandroid:name="android.permission.SCHEDULE_EXACT_ALARM"/><uses-permissionandroid:name="android.permission.USE_EXACT_ALARM"/><!-- 备用权限 -->2. 检查并申请精确闹钟权限(Kotlin)
privatefuncheckExactAlarmPermission(){valalarmManager=getSystemService(AlarmManager::class.java)if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.S){// 检查权限是否开启if(!alarmManager.canScheduleExactAlarms()){// 跳转到应用权限设置页面valintent=Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM,Uri.parse("package:$packageName"))startActivityForResult(intent,REQUEST_EXACT_ALARM)}else{// 权限已开启,设置精确闹钟setExactAlarm()}}else{setExactAlarm()}}// 设置精确闹钟privatefunsetExactAlarm(){valalarmManager=getSystemService(AlarmManager::class.java)valintent=Intent(this,AlarmReceiver::class.java)valpendingIntent=PendingIntent.getBroadcast(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENTorPendingIntent.FLAG_IMMUTABLE// Android 12 要求 FLAG_IMMUTABLE)valtriggerTime=System.currentTimeMillis()+60*1000// 1分钟后if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,triggerTime,pendingIntent)}else{alarmManager.setExact(AlarmManager.RTC_WAKEUP,triggerTime,pendingIntent)}}3. PendingIntent 行为变更
Android 12 要求创建PendingIntent时必须指定FLAG_IMMUTABLE或FLAG_MUTABLE,否则会抛出异常。
适配要点:
- 所有 PendingIntent 创建时添加
FLAG_IMMUTABLE(默认推荐) - 仅当需要修改 PendingIntent 内容时使用
FLAG_MUTABLE
代码示例:
// 正确写法(Android 12+)valpendingIntent=PendingIntent.getActivity(this,0,intent,// 组合 FLAG_UPDATE_CURRENT 和 FLAG_IMMUTABLEPendingIntent.FLAG_UPDATE_CURRENTorPendingIntent.FLAG_IMMUTABLE)// 若需要修改 PendingIntent 内容(慎用)valmutablePendingIntent=PendingIntent.getBroadcast(this,1,intent,PendingIntent.FLAG_UPDATE_CURRENTorPendingIntent.FLAG_MUTABLE)4. 应用启动画面(Splash Screen)
Android 12 引入了标准化的启动画面 API,替代传统的 SplashActivity 方案。
适配要点:
- 使用 SplashScreen 库适配
- 配置启动画面样式
- 控制启动画面消失时机
代码示例:
1. 依赖引入(build.gradle)
dependencies { implementation "androidx.core:core-splashscreen:1.0.1" }2. 样式配置(res/values/styles.xml)
<stylename="Theme.App.SplashScreen"parent="Theme.SplashScreen"><!-- 启动画面背景色 --> <item name="windowSplashScreenBackground">@color/white</item> <!-- 应用图标 --> <item name="windowSplashScreenAnimatedIcon">@mipmap/ic_launcher</item> <!-- 图标动画时长(可选) --> <item name="windowSplashScreenAnimationDuration">1000</item> <!-- 启动画面结束后跳转的主题 --> <item name="postSplashScreenTheme">@style/Theme.App</item></style>3. Manifest 配置
<activityandroid:name=".MainActivity"android:theme="@style/Theme.App.SplashScreen"><intent-filter><actionandroid:name="android.intent.action.MAIN"/><categoryandroid:name="android.intent.category.LAUNCHER"/></intent-filter></activity>4. 代码控制(MainActivity.kt)
classMainActivity:AppCompatActivity(){overridefunonCreate(savedInstanceState:Bundle?){super.onCreate(savedInstanceState)// 安装启动画面installSplashScreen()// 模拟初始化耗时操作CoroutineScope(Dispatchers.Main).launch{delay(2000)// 2秒后消失setContentView(R.layout.activity_main)}}}5. 触摸反馈和滚动行为优化
Android 12 增强了触摸反馈和滚动效果,可通过 Material Design 3 适配。
适配要点:
- 升级到 Material Design 3
- 配置触摸反馈样式
代码示例:
1. 依赖引入
implementation "com.google.android.material:material:1.8.0"2. 样式配置(res/values/styles.xml)
<stylename="Theme.App"parent="Theme.Material3.DayNight.NoActionBar"><!-- 触摸反馈颜色 --> <item name="android:colorControlHighlight">@color/touch_feedback</item> <!-- 滚动条样式 --> <item name="android:scrollbarStyle">insideOverlay</item></style>三、其他重要适配点
| 特性 | 适配要点 |
|---|---|
| 应用兼容性 | 禁用android:hardwareAccelerated="false"(部分设备会导致崩溃) |
| 剪贴板访问 | 访问剪贴板时会弹出系统提示,避免后台频繁访问 |
| 网络权限 | NETWORK_WIFI等权限需配合ACCESS_NETWORK_STATE使用 |
| 后台启动限制 | 进一步收紧后台启动 Activity,优先使用通知引导用户手动打开 |
四、测试建议
- 使用 Android 12 真机或模拟器测试
- 重点测试权限申请流程、前台服务、闹钟功能
- 检查第三方库是否适配 Android 12(如推送、统计SDK)
- 使用 Lint 工具检测适配问题:
./gradlew lint
五、总结
Android 12 的适配核心围绕权限收紧、PendingIntent 标记、前台服务限制和新 API 适配展开。建议分阶段适配:
- 优先修复崩溃类问题(如 PendingIntent 缺少标记)
- 适配核心功能权限(前台服务、精确闹钟)
- 接入新特性(启动画面、Material Design 3)
- 全面测试并优化用户体验
六、参考资源
- 安卓12官方文档
- 安卓12行为变更
通过以上适配,可确保应用在 Android 12 设备上稳定运行,并充分利用新特性提升用户体验。