提供手机网站制作哪家好,做网站的网站违不违法,重庆seo推广渠道,景区网站建设的意义Android 12#xff08;API 31#xff09;引入了多项重要特性和行为变更#xff0c;本文从开发视角梳理核心适配点#xff0c;结合 Kotlin 代码示例说明具体实现方式。
一、核心适配前提
升级开发环境#xff1a; Gradle 插件 ≥ 7.0.0compileSdkVersion ≥ 31targetSdkV…Android 12API 31引入了多项重要特性和行为变更本文从开发视角梳理核心适配点结合 Kotlin 代码示例说明具体实现方式。一、核心适配前提升级开发环境Gradle 插件 ≥ 7.0.0compileSdkVersion ≥ 31targetSdkVersion 建议升级到 31若暂不升级部分特性仍需适配依赖库升级确保 AppCompat、Material 等库版本 ≥ 1.4.0二、关键特性适配1. 前台服务Foreground Service限制Android 12 收紧了前台服务权限仅允许特定场景使用且新增POST_NOTIFICATIONS权限。适配要点声明前台服务类型必须在 Manifest 中指定动态申请通知权限Android 13 强制12 建议提前适配非允许场景使用 WorkManager 替代代码示例1. Manifest 配置!-- 声明前台服务类型 --serviceandroid:name.MyForegroundServiceandroid:foregroundServiceTypelocation|mediaPlayback|phoneCall/!-- 声明通知权限 --uses-permissionandroid:nameandroid.permission.POST_NOTIFICATIONS/2. 动态申请通知权限Kotlin// 检查并申请通知权限privatefunrequestNotificationPermission(){if(Build.VERSION.SDK_INTBuild.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(){valserviceIntentIntent(this,MyForegroundService::class.java)if(Build.VERSION.SDK_INTBuild.VERSION_CODES.O){startForegroundService(serviceIntent)}else{startService(serviceIntent)}}3. 前台服务实现KotlinclassMyForegroundService:Service(){privatevalNOTIFICATION_ID1001privatevalCHANNEL_IDFOREGROUND_CHANNELoverridefunonStartCommand(intent:Intent?,flags:Int,startId:Int):Int{createNotificationChannel()valnotificationNotificationCompat.Builder(this,CHANNEL_ID).setContentTitle(前台服务).setContentText(服务运行中).setSmallIcon(R.mipmap.ic_launcher).build()// Android 12 必须指定服务类型if(Build.VERSION.SDK_INTBuild.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_INTBuild.VERSION_CODES.O){valchannelNotificationChannel(CHANNEL_ID,前台服务通道,NotificationManager.IMPORTANCE_LOW)valmanagergetSystemService(NotificationManager::class.java)manager.createNotificationChannel(channel)}}overridefunonBind(intent:Intent?):IBinder?null}2. 精确闹钟权限SCHEDULE_EXACT_ALARMAndroid 12 要求使用精确闹钟setExact()/setExactAndAllowWhileIdle()必须申请SCHEDULE_EXACT_ALARM权限。适配要点Manifest 声明权限运行时检查并申请权限处理权限被拒绝的场景代码示例1. Manifest 配置uses-permissionandroid:nameandroid.permission.SCHEDULE_EXACT_ALARM/uses-permissionandroid:nameandroid.permission.USE_EXACT_ALARM/!-- 备用权限 --2. 检查并申请精确闹钟权限KotlinprivatefuncheckExactAlarmPermission(){valalarmManagergetSystemService(AlarmManager::class.java)if(Build.VERSION.SDK_INTBuild.VERSION_CODES.S){// 检查权限是否开启if(!alarmManager.canScheduleExactAlarms()){// 跳转到应用权限设置页面valintentIntent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM,Uri.parse(package:$packageName))startActivityForResult(intent,REQUEST_EXACT_ALARM)}else{// 权限已开启设置精确闹钟setExactAlarm()}}else{setExactAlarm()}}// 设置精确闹钟privatefunsetExactAlarm(){valalarmManagergetSystemService(AlarmManager::class.java)valintentIntent(this,AlarmReceiver::class.java)valpendingIntentPendingIntent.getBroadcast(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENTorPendingIntent.FLAG_IMMUTABLE// Android 12 要求 FLAG_IMMUTABLE)valtriggerTimeSystem.currentTimeMillis()60*1000// 1分钟后if(Build.VERSION.SDK_INTBuild.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 12valpendingIntentPendingIntent.getActivity(this,0,intent,// 组合 FLAG_UPDATE_CURRENT 和 FLAG_IMMUTABLEPendingIntent.FLAG_UPDATE_CURRENTorPendingIntent.FLAG_IMMUTABLE)// 若需要修改 PendingIntent 内容慎用valmutablePendingIntentPendingIntent.getBroadcast(this,1,intent,PendingIntent.FLAG_UPDATE_CURRENTorPendingIntent.FLAG_MUTABLE)4. 应用启动画面Splash ScreenAndroid 12 引入了标准化的启动画面 API替代传统的 SplashActivity 方案。适配要点使用 SplashScreen 库适配配置启动画面样式控制启动画面消失时机代码示例1. 依赖引入build.gradledependencies { implementation androidx.core:core-splashscreen:1.0.1 }2. 样式配置res/values/styles.xmlstylenameTheme.App.SplashScreenparentTheme.SplashScreen!-- 启动画面背景色 -- item namewindowSplashScreenBackgroundcolor/white/item !-- 应用图标 -- item namewindowSplashScreenAnimatedIconmipmap/ic_launcher/item !-- 图标动画时长可选 -- item namewindowSplashScreenAnimationDuration1000/item !-- 启动画面结束后跳转的主题 -- item namepostSplashScreenThemestyle/Theme.App/item/style3. Manifest 配置activityandroid:name.MainActivityandroid:themestyle/Theme.App.SplashScreenintent-filteractionandroid:nameandroid.intent.action.MAIN/categoryandroid:nameandroid.intent.category.LAUNCHER//intent-filter/activity4. 代码控制MainActivity.ktclassMainActivity: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.02. 样式配置res/values/styles.xmlstylenameTheme.AppparentTheme.Material3.DayNight.NoActionBar!-- 触摸反馈颜色 -- item nameandroid:colorControlHighlightcolor/touch_feedback/item !-- 滚动条样式 -- item nameandroid:scrollbarStyleinsideOverlay/item/style三、其他重要适配点特性适配要点应用兼容性禁用android:hardwareAcceleratedfalse部分设备会导致崩溃剪贴板访问访问剪贴板时会弹出系统提示避免后台频繁访问网络权限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 设备上稳定运行并充分利用新特性提升用户体验。