天天看点

Android12 新特性及适配指南

Android 12(API 31)于2021年10月4日正式发布,正式版源代码也于当日被推送到AOSP Android开源项目。截止到笔者撰写这篇文章时,国内各终端厂商的在售Android设备,已经逐步开启了Android 12正式版本的更新。当前,对于Android应用开发者来说,Android 12 的软件兼容适配已迫在眉睫。

Android 12(API 31)于2021年10月4日正式发布,正式版源代码也于当日被推送到AOSP Android开源项目。截止到笔者撰写这篇文章时,国内各终端厂商的在售Android设备,已经逐步开启了Android 12正式版本的更新。当前,对于Android应用开发者来说,Android 12 的软件兼容适配已迫在眉睫。

对于

Android 12

的兼容适配,主要分为两类:

一类默认影响所有运行的应用

另一类则只对声明 targetSdkVersion 31 的应用产生影响。

对于Android 12 的

适配点

,这里按照以下几个方面进行了归纳:

  • 新的应用启动页:

    应用启动页

    SplashScreen

    (影响所有应用);
  • 声明

    android:exported

    应用组件需显示声明

    android:exported

    (以Android12位目标平台的应用);
  • Alarm

    精确闹钟

    应用程序使用

    Alarm精确闹钟

    需申请

    SCHEDULE_EXACT_ALARM

    权限(以Android12位目标平台的应用);
  • 通知栏变更:

    Notification

    通知栏布局样式

    再次调整(以Android12位目标平台的应用);
  • 精确位置

    请求精确位置,需同时申请

    ACCESS_FINE_LOCATION

    ACCESS_COARSE_LOCATION

    权限(以Android12位目标平台的应用);
  • 前台服务:

    禁止从后台启动前台服务

    (以Android12位目标平台的应用);
  • 蓝牙权限:

    申请蓝牙相关权限时,

    不再需要申请设备位置信息相关权限

    (以Android12位目标平台的应用);

官方文档描述:https://developer.android.google.cn/about/versions/12

一、应用启动页

(Android 启动页 SplashScreen:影响所有应用)

从 Android 12 开始,系统会在应用的

冷启动和暖启动

时,使用新的启动页

SplashScreen

,该启动页默认由

应用ICON

+

应用主题的windowBackground

内容构成。

Android12 新特性及适配指南

影响在 Andorid 12 设备上运行的所有应用

SplashScreen相关API的引入

影响在Andorid 12设备上运行的所有应用

。对于应用开发者来说,无论你的应用

targetSdkVersion

版本是多少,均需要进行

SplashScreen

的适配工作。

若未进行 SplashScreen 的适配工作

若开发者未进行SplashScreen的适配工作,当应用运行于Android 12及以上版本的设备,在应用的

冷启动 或 温启动

时:

  • 若你的应用原本使用 android:windowBackground 实现了启动页,会被默认的启动页样式替换。
  • 若你的应用使用了一个额外的 Activity 作为启动页,则会先弹出系统默认启动页,再弹出你实现的启动页 (用户可能会感受到两次闪屏效果)。

SplashScreen 自定义与案例代码:

新的启动页中的

显示元素

可完全由由

开发者自定义

,官方建议开发者:将未适配Android12前前的应用启动页完全移除,并适配Android12新的启动页,从而避免启动页重复、减少加载时间的问题。

关于 SplashScreen 适配相关API的

详细案例代码

API使用说明

请参考文章:

Android 12 适配指南——SplashScreen

https://xiaxl.blog.csdn.net/article/details/123522277

二、android:exported

(显示声明 android:exported:影响以Android 12为目标平台的应用「targetSdkVersion 31」)

从 Andorid 12 开始,当您的应用程序将目标版本设置为31或更高版本(

targetSdkVersion 31

)时,若应用程序组件(

Activity

Service

Receiver

Provider

)在配置清单manifest中未显示声明 android:exported 属性,则在进行应用开发或打包时,将会出现如下错误提示:

As of Android 12, android:exported must be set; use true to make the activity available to other apps, and false otherwise. For launcher activities, this should be set to true.

对于这一点的更改,官方描述如下图所示:

Android12 新特性及适配指南

android:exported = true的作用?

当应用程序组件,需要被另一个应用(Application)的组件启动或调用时:true 允许调用;false 不允许其他应用启动或调用。例如:在Activity中用来标示:当前Activity是否可以被另一个Application的组件启动;

因此,在Android 12中需显示声明 android:exported 属性,举例如下:

// launcher Activity 需将exported设置为true
<activity
    android:name=".SplashActivity"
    android:exported="true"
    android:theme="@style/Theme.SplashScreen.Demo">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
// 非launcher Activity 又无外部启动需求设置为fase
<activity
    android:name=".MainActivity"
    android:exported="false">
</activity>
           

三、Alarm精确闹钟

(Alarm精确闹钟:影响以Android 12为目标平台的应用「targetSdkVersion 31」)

从 Andorid 12 开始,当您的应用程序将目标版本设置为31或更高版本(

targetSdkVersion 31

)时,若您的应用程序需要使用精确闹钟,需申请一个新的权限(闹钟和提醒权限),该权限为普通权限,无需动态申请:

<!--Android S alarm permission-->
<!--普通权限:无需动态申请-->
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
           

对于这一点的更改,官方描述如下图所示:

Android12 新特性及适配指南

添加该权限的原因是:

Android官方为了节省系统资源,希望应用开发者尽可能将应用调整为不再需要使用精确闹钟的状态,从而减少应用闹钟对操作系统的频繁唤醒。

Android12 新特性及适配指南

四、Notification通知栏

(Notification通知栏:影响以Android 12为目标平台的应用「targetSdkVersion 31」)

从 Andorid 12 开始,系统再次更改了自定义通知栏的布局方式和样式。

  • Android 12以前,自定义通知栏能够使用整个通知区域并自定义自己的布局样式;因此,在不同设备上可能出现布局兼容问题;
  • Android12开始,对于以Android 12 为目标平台的应用,通知栏的自定义试图将不再使用完整的通知栏区域。

    系统会提供标准模板

    ,此模板可确保自定义通知栏在所有状态下效果保持一致。
Android12 新特性及适配指南

Android 12开始,系统提供

Notification.DecoratedCustomViewStyle

通知栏样式,用于展示与构建收起与展开状态的通知栏样式。

标准模板自定义通知栏的展示样式,如下图所示:

Android12 新特性及适配指南

标准通知栏的收起状态:

Android12 新特性及适配指南

custom-collapsed-view.jpg

标准通知栏的展开状态:

Android12 新特性及适配指南

相关代码的使用方式如下:

// Get the layouts to use in the custom notification
val notificationSmallLayout = RemoteViews(packageName, R.layout.notification_small)
val notificationLargeLayoutExpanded = RemoteViews(packageName, R.layout.notification_large)
// Apply the layouts to the notification
val customNotification = NotificationCompat.Builder(context, channelId)
    // icon
    .setSmallIcon(R.drawable.ic_launcher)
    // style
    .setStyle(NotificationCompat.DecoratedCustomViewStyle())
    // 设置收起后通知布局
    .setCustomContentView(notificationSmallLayout)
    // 设置展后的通知布局
    .setCustomBigContentView(notificationLargeLayoutExpanded)
    .build()
notificationManager.notify(1, customNotification);
           

注:通知的背景颜色可能会因设备和系统版本而有所差异。因此,开始在在自定义布局中建议对文本使用Style:

TextAppearance_Compat_Notification

,对标题使用Style:

TextAppearance_Compat_Notification_Title

。以上样式会适应系统的颜色变化,不会出现黑色文本采用黑色背景或白色文本采用白色背景的情况。

举例如下:

<TextView
    android:id="@+id/notification_title"
    style="@style/TextAppearance.Compat.Notification.Title"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:text="notification_small" />
           

通知栏 相关官方文档参考:

Android 12 中的兼容性变更:

https://mp.weixin.qq.com/s/ek2UT0vauTeVQQF0K5fkSQ

Android 12 行为变更——自定义通知:

https://developer.android.google.cn/about/versions/12/behavior-changes-12?hl=zh-cn

developer自定义通知栏:

https://developer.android.google.cn/training/notify-user/custom-notification?hl=zh-cn#kotlin

五、精确位置

(精确位置:影响以Android 12为目标平台的应用「targetSdkVersion 31」)

从 Andorid 12 开始,当您的应用程序将目标版本设置为31或更高版本(

targetSdkVersion 31

)时,若应用程序请求设备的

精确位置

,需同时请求

ACCESS_FINE_LOCATION

ACCESS_COARSE_LOCATION

权限。

发出精确位置申请后,用户侧设备将弹出动态授权申请弹窗:

Android12 新特性及适配指南

若开发者只请求ACCESS_FINE_LOCATION权限,将弹出以下错误提示:

ACCESS_FINE_LOCATION must be requested with ACCESS_COARSE_LOCATION.
           

精确位置 相关官方文档参考:

developer位置授权:

https://developer.android.google.cn/training/location/permissions?hl=zh-cn#approximate-request

Android 12行为变更——大致位置:

https://developer.android.google.cn/about/versions/12/behavior-changes-12?hl=zh-cn

六、前台服务

(前台服务:影响以Android 12为目标平台的应用「targetSdkVersion 31」)

从 Andorid 12 开始,当您的应用程序将目标版本设置为31或更高版本(

targetSdkVersion 31

)时,将

禁止从后台启动前台服务

,并对启动前台服务作了限制。

调整后,以下情况可启动前台服务:

  • 可见的 Activity 或窗口;
  • 用户操作,如通知、小部件等等;
  • 特定的广播和回调;
  • STICKY 类型的服务可在崩溃或由于低内存而停止运行的情况下重启;

七、蓝牙权限

(蓝牙权限:影响以Android 12为目标平台的应用「targetSdkVersion 31」)

Android 12 引入了

BLUETOOTH_SCAN

BLUETOOTH_ADVERTISE

BLUETOOTH_CONNECT

权限。这些权限可让以 Android 应用更轻松地与蓝牙设备互动,

不再需要申请设备位置信息相关权限

Android 12 开始,Google官方将

蓝牙扫描

位置权限

进行了分离,因为官方发现:

在隐私层面上,很难向终端用户解释位置权限与蓝牙的关系

参考

Android developer:Andoid12

https://developer.android.google.cn/about/versions/12?hl=zh-cn

Android开发者:Android 12 正式发布

https://mp.weixin.qq.com/s/OiFSWEnc-0N2z7JYWTJluw

AOSP:Android 开源项目

https://source.android.google.cn/

Material You:

https://material.io/blog/announcing-material-you

Material 设计组件:

https://github.com/material-components/material-components-android/releases

androidx releases core:

https://developer.android.com/jetpack/androidx/releases/core?hl=zh-cn

= THE END =

文章首发于公众号”CODING技术小馆“,如果文章对您有帮助,欢迎关注我的公众号。