1. 程式人生 > 其它 >Android12 新特性及適配指南

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

影響在 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 = 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_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技術小館“,如果文章對您有幫助,歡迎關注我的公眾號。