打赏

记录Android S 适配

Android S 适配

最近在做app的Android S适配,targetSDK切到了31,记录一下适配的主要内容
compileSdkVersion = 'android-S'
minSdkVersion = 31
targetSdkVersion = 'S'

Android manifest

带有 intents-filter 的组件(activity,receiver,provider,service)必须显示的
设置 android:exported, 否则 app 无法安装
https://developer.android.com/about/versions/12/behavior-changes-12#exported

Pending intent

创建 Pending intent 的时候,必须加上声明mutability的flag,PendingIntent.FLAG_MUTABLE 或者 PendingIntent.FLAG_IMMUTABLE,
通常情况使用 PendingIntent.FLAG_IMMUTABLE
https://developer.android.com/about/versions/12/behavior-changes-12#pending-intent-mutability

Exact alarm permission

如果app用到Alarmmanager 的设置准点alarm的接口,如:setAlarmClock() setExact()
setExactAndAllowWhileIdle(),需要在manifest中申明使用android.permission.SCHEDULE_EXACT_ALARM权限
当持有这个权限的时候才可以使用这些接口,这个权限用户可以通过系统设置中的开关收回,权限如果被收回,之前调用这些接口设置的alarm都会被取消,所以需要做几个事情:

  • 使用这些接口前通过 canScheduleExactAlarms() 检查当前权限是否授予。
  • 实现一个Broadcast Receiver监听android.app.action.SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED广播,当用户在设置中打开开关授予权限时会发送这个广播,
    app收到广播并确认权限授予后,要把需要设置的alarm再重新设置一遍,类似收到 BOOT_COMPLETED时的操作
  • 如果app发现权限未被授予,可以提示用户,并通过ACTION_REQUEST_SCHEDULE_EXACT_ALARM引导用户打开开关

https://developer.android.com/about/versions/12/behavior-changes-12#exact-inexact-alarms

Notification trampoline restriction

用户点击了一个notification或者notification上的一个action button,如果pendingintent调起
的是receiver或者service,那么在这个receiver或者service中,不能调用startActivity()来调起
一个Activity。

解决办法:在创建notification时传入的pendingintent直接指向对应的activity。
如果之前service中所做的工作比较多,可以用一个透明activity作为跳转,即notification先跳转到这个activity,activity中再把之前的service调起,然后activity finish。
https://developer.android.com/about/versions/12/behavior-changes-12#notification-trampolines

Custom notification

自定义通知中的自定义布局区域有变化, GUI 可能需要调整:

 
img-paste-2021070209474910.png

https://developer.android.com/about/versions/12/behavior-changes-12#custom-notifications

 

Approximate location

App 如果动态申请ACCESS_FINE_LOCATION权限,必须同时也申请ACCESS_COARSE_LOCATION权限。当同时申请这两个权限时,系统权限对话框会允许用户选择授予精确定位权限还是粗略定位权限:

 
img-approximate-location.png

https://developer.android.com/about/versions/12/behavior-changes-12#approximate-location

 

posted @ 2022-01-14 14:24  张学涛  阅读(320)  评论(0编辑  收藏  举报