Android12 新特性及適配指南


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_LOCATIONACCESS_COARSE_LOCATION 權限(以Android12位目標平台的應用);
  • 前台服務:
    禁止從后台啟動前台服務(以Android12位目標平台的應用);
  • 藍牙權限:
    申請藍牙相關權限時,不再需要申請設備位置信息相關權限(以Android12位目標平台的應用);

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

一、應用啟動頁

(Android 啟動頁 SplashScreen:影響所有應用)

從 Android 12 開始,系統會在應用的冷啟動和暖啟動時,使用新的啟動頁 SplashScreen,該啟動頁默認由應用ICON + 應用主題的windowBackground內容構成。

SplashScreen 啟動頁運行效果

影響在 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)時,若應用程序組件(ActivityServiceReceiverProvider)在配置清單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.

對於這一點的更改,官方描述如下圖所示:
官方對於 android:exported 的詳細描述

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" />

對於這一點的更改,官方描述如下圖所示:
官方對於 Alarm精確鬧鍾 的詳細描述

添加該權限的原因是:Android官方為了節省系統資源,希望應用開發者盡可能將應用調整為不再需要使用精確鬧鍾的狀態,從而減少應用鬧鍾對操作系統的頻繁喚醒。

Android 12 精確鬧鍾權限

四、Notification通知欄

(Notification通知欄:影響以Android 12為目標平台的應用「targetSdkVersion 31」)

從 Andorid 12 開始,系統再次更改了自定義通知欄的布局方式和樣式。

  • Android 12以前,自定義通知欄能夠使用整個通知區域並自定義自己的布局樣式;因此,在不同設備上可能出現布局兼容問題;
  • Android12開始,對於以Android 12 為目標平台的應用,通知欄的自定義試圖將不再使用完整的通知欄區域。系統會提供標准模板,此模板可確保自定義通知欄在所有狀態下效果保持一致。

Android12通知欄調整

Android 12開始,系統提供Notification.DecoratedCustomViewStyle通知欄樣式,用於展示與構建收起與展開狀態的通知欄樣式。

標准模板自定義通知欄的展示樣式,如下圖所示:
標准模板自定義通知欄的展示樣式

標准通知欄的收起狀態:
通知欄的收起狀態
custom-collapsed-view.jpg
標准通知欄的展開狀態:
標准通知欄的展開狀態

相關代碼的使用方式如下:

// 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_LOCATIONACCESS_COARSE_LOCATION 權限。
發出精確位置申請后,用戶側設備將彈出動態授權申請彈窗:

精確權限動態授權彈窗

若開發者只請求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_SCANBLUETOOTH_ADVERTISEBLUETOOTH_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技術小館“,如果文章對您有幫助,歡迎關注我的公眾號。
歡迎關注我的公眾號


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM