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內容構成,

影響在 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.
對于這一點的更改,官方描述如下圖所示:

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" />
對于這一點的更改,官方描述如下圖所示:

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

四、Notification通知欄
(Notification通知欄:影響以Android 12為目標平臺的應用「targetSdkVersion 31」)
從 Andorid 12 開始,系統再次更改了自定義通知欄的布局方式和樣式,
- Android 12以前,自定義通知欄能夠使用整個通知區域并自定義自己的布局樣式;因此,在不同設備上可能出現布局兼容問題;
- Android12開始,對于以Android 12 為目標平臺的應用,通知欄的自定義試圖將不再使用完整的通知欄區域,
系統會提供標準模板,此模板可確保自定義通知欄在所有狀態下效果保持一致,

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

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

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/445572.html
標籤:Android
