和先前版本一樣,Android 15 也包含可能會影響應用程式的行為變更。以下行為變更僅適用於指定 Android 15 以上版本的應用程式。如果您的應用程式指定 Android 15 以上版本,建議您視情況修改應用程式,以支援這些行為。
此外,無論應用程式的 targetSdkVersion 為何,請務必查看對所有 Android 15 應用程式有影響的行為變更清單。
核心功能
Android 15 修改或擴充了 Android 系統的各種核心功能。
前景服務異動
We are making the following changes to foreground services with Android 15.
- Data sync foreground service timeout behavior
- New media processing foreground service type
- Restrictions on
BOOT_COMPLETEDbroadcast receivers launching foreground services - Restrictions on starting foreground services while an app holds the
SYSTEM_ALERT_WINDOWpermission
Data sync foreground service timeout behavior
Android 15 為指定 Android 15 (API 級別 35) 以上版本為目標版本的應用程式,在 dataSync 中導入新的逾時行為。這項行為也適用於新的mediaProcessing前景服務類型。
系統允許應用程式的 dataSync 服務在 24 小時內執行總共 6 小時,之後系統會呼叫執行中的服務 Service.onTimeout(int, int) 方法 (在 Android 15 中推出)。此時,服務有幾秒的時間可以呼叫 Service.stopSelf()。呼叫 Service.onTimeout() 後,系統就不會再將服務視為前景服務。如果服務未呼叫 Service.stopSelf(),系統會擲回內部例外狀況。例外狀況會記錄在 Logcat 中,並顯示以下訊息:
Fatal Exception: android.app.RemoteServiceException: "A foreground service of
type dataSync did not stop within its timeout: [component name]"
如要避免這項行為變更帶來的問題,您可以執行下列一或多項操作:
- 讓您的服務實作新的
Service.onTimeout(int, int)方法。應用程式收到回呼時,請務必在幾秒內呼叫stopSelf()。(如果您沒有立即停止應用程式,系統會產生失敗)。 - 確認應用程式的
dataSync服務在任何 24 小時內的執行時間不超過 6 小時 (除非使用者與應用程式互動,並重設計時器)。 - 只有在使用者直接互動時才啟動
dataSync前景服務;由於您的應用程式在服務啟動後位於前景,因此應用程式進入背景後,您的服務剩下六小時。 - 請改用其他 API,而非
dataSync前景服務。
如果應用程式的 dataSync 前景服務在過去 24 小時內已執行 6 小時,您就無法啟動其他 dataSync 前景服務,除非使用者將應用程式移至前景 (這樣會重設計時器)。如果您嘗試啟動另一項 dataSync 前景服務,系統會擲回 ForegroundServiceStartNotAllowedException,並顯示錯誤訊息「Time limit is Of for 前景服務類型 dataSync」
測試
如要測試應用程式的行為,您可以啟用資料同步處理逾時,即使應用程式並非以 Android 15 為目標版本 (只要應用程式是在 Android 15 裝置上執行),也能啟用。如要啟用逾時值,請執行下列 adb 指令:
adb shell am compat enable FGS_INTRODUCE_TIME_LIMITS your-package-name
您也可以調整逾時期限,方便測試應用程式在達到限制時的行為。如要設定新的逾時期限,請執行下列 adb 指令:
adb shell device_config put activity_manager data_sync_fgs_timeout_duration duration-in-milliseconds
New media processing foreground service type
Android 15 推出了新的前景服務類型 mediaProcessing。此服務類型適合用於轉碼媒體檔案等作業。舉例來說,媒體應用程式可能會下載音訊檔案,需要將其轉換成其他格式後再播放。您可以使用 mediaProcessing 前景服務,確保即使應用程式處於背景執行狀態,轉換也能繼續進行。
系統允許應用程式的 mediaProcessing 服務在 24 小時內執行總共 6 小時,之後系統會呼叫執行中的服務 Service.onTimeout(int, int) 方法 (在 Android 15 中推出)。此時,服務有幾秒的時間可以呼叫 Service.stopSelf()。如果服務未呼叫 Service.stopSelf(),系統會擲回內部例外狀況。例外狀況會記錄在 Logcat 中,並顯示以下訊息:
Fatal Exception: android.app.RemoteServiceException: "A foreground service of
type mediaProcessing did not stop within its timeout: [component name]"
為避免例外狀況,您可以採取下列其中一種做法:
- 請讓服務實作新的
Service.onTimeout(int, int)方法。應用程式收到回呼時,請務必在幾秒內呼叫stopSelf()。(如果您沒有立即停止應用程式,系統會產生失敗)。 - 請確認應用程式的
mediaProcessing服務在任何 24 小時內的總執行時間不超過 6 小時 (除非使用者與應用程式互動,重新設定計時器)。 - 只有在使用者直接互動時才啟動
mediaProcessing前景服務;由於您的應用程式在服務啟動後位於前景,因此應用程式進入背景後,您的服務剩下六小時。 - 請改用 WorkManager 等其他 API,而非
mediaProcessing前景服務。
如果應用程式的 mediaProcessing 前景服務在過去 24 小時內已執行 6 小時,您就無法啟動其他 mediaProcessing 前景服務,除非使用者將應用程式移至前景 (這樣會重設計時器)。如果您嘗試啟動其他 mediaProcessing 前景服務,系統會擲回 ForegroundServiceStartNotAllowedException,並顯示「前景服務類型 mediaProcessing 的時間限制已用盡」等錯誤訊息。
如要進一步瞭解 mediaProcessing 服務類型,請參閱「Android 15 的前景服務類型變更:媒體處理」。
測試
如要測試應用程式的行為,您可以啟用媒體處理逾時,即使應用程式並未以 Android 15 為目標版本 (只要應用程式在 Android 15 裝置上執行),也能啟用。如要啟用逾時值,請執行下列 adb 指令:
adb shell am compat enable FGS_INTRODUCE_TIME_LIMITS your-package-name
您也可以調整逾時期限,方便測試應用程式在達到上限時的行為。如要設定新的逾時期限,請執行下列 adb 指令:
adb shell device_config put activity_manager media_processing_fgs_timeout_duration duration-in-milliseconds
Restrictions on BOOT_COMPLETED broadcast receivers launching foreground services
BOOT_COMPLETED 廣播接收器啟動有一些新限制
前景服務BOOT_COMPLETED 接收器無法啟動
下列類型的前景服務:
dataSynccameramediaPlaybackphoneCallmediaProjectionmicrophone(我們已對microphone設下這項限制,自 Android 14)
如果 BOOT_COMPLETED 接收器嘗試啟動任何這些類型的前景
服務就會擲回 ForegroundServiceStartNotAllowedException。
測試
如要測試應用程式的行為,即使應用程式並非以 Android 15 為目標版本 (只要應用程式是在 Android 15 裝置上執行),您也可以啟用這些新限制。執行下列 adb 指令:
adb shell am compat enable FGS_BOOT_COMPLETED_RESTRICTIONS your-package-name
如要在不重新啟動裝置的情況下傳送「BOOT_COMPLETED」廣播訊息,請按照下列步驟操作:
執行下列 adb 指令:
adb shell am broadcast -a android.intent.action.BOOT_COMPLETED your-package-name
Restrictions on starting foreground services while an app holds the SYSTEM_ALERT_WINDOW permission
以前,如果应用拥有 SYSTEM_ALERT_WINDOW 权限,即使应用当前在后台运行,也可以启动前台服务(如免于后台启动限制中所述)。
如果应用以 Android 15 为目标平台,则此豁免范围现在更窄。现在,应用需要具有 SYSTEM_ALERT_WINDOW 权限,并且还需要有一个可见的叠加窗口。也就是说,应用需要先启动 TYPE_APPLICATION_OVERLAY 窗口,并且该窗口需要处于可见状态,然后您才能启动前台服务。
如果您的应用尝试从后台启动前台服务,但不符合这些新要求(并且没有其他豁免情况),系统会抛出 ForegroundServiceStartNotAllowedException。
如果您的应用声明了 SYSTEM_ALERT_WINDOW 权限并从后台启动前台服务,则可能会受到此变更的影响。如果您的应用获得了 ForegroundServiceStartNotAllowedException,请检查应用的操作顺序,并确保应用在尝试从后台启动前台服务之前已具有有效的叠加层窗口。您可以通过调用 View.getWindowVisibility() 检查叠加层窗口当前是否可见,也可以替换 View.onWindowVisibilityChanged(),以便在可见性发生变化时收到通知。
测试
如需测试应用的行为,您可以启用这些新限制,即使您的应用并未以 Android 15 为目标平台(只要应用在 Android 15 设备上运行)也是如此。如需针对从后台启动前台服务启用这些新限制,请运行以下 adb 命令:
adb shell am compat enable FGS_SAW_RESTRICTIONS your-package-name
變更應用程式修改零打擾模式全域狀態的時間
以 Android 15(API 级别 35)及更高版本为目标平台的应用无法再更改设备上的勿扰 (DND) 功能的全局状态或政策(无论是通过修改用户设置还是关闭勿扰模式)。相反,应用必须提供 AutomaticZenRule,系统会将其与现有的“最严格的政策优先”方案合并为一个全局政策。对之前会影响全局状态的现有 API 的调用(setInterruptionFilter、setNotificationPolicy)会导致创建或更新隐式 AutomaticZenRule,该 AutomaticZenRule 会根据这些 API 调用的调用周期开启和关闭。
请注意,只有当应用调用 setInterruptionFilter(INTERRUPTION_FILTER_ALL) 并希望该调用停用之前由其所有者激活的 AutomaticZenRule 时,此更改才会影响可观察到的行为。
OpenJDK API 變更
Android 15 持續更新 Android 核心程式庫,以便與最新版 OpenJDK LTS 中的功能保持一致。
如果應用程式指定 Android 15 (API 級別 35),以下部分變更可能會影響應用程式相容性:
字串格式化 API 的變更:使用下列
String.format()和Formatter.format()API 時,系統現在會更嚴格地驗證引數索引、旗標、寬度和精確度:String.format(String, Object[])String.format(Locale, String, Object[])Formatter.format(String, Object[])Formatter.format(Locale, String, Object[])
舉例來說,使用引數索引 0 (格式字串中的
%0) 時,會擲回下列例外狀況:IllegalFormatArgumentIndexException: Illegal format argument index = 0在這種情況下,使用引數索引 1 (格式字串中的
%1) 即可修正問題。Arrays.asList(...).toArray()的元件類型變更:使用Arrays.asList(...).toArray()時,產生的陣列元件類型現在是Object,而不是基礎陣列元素的類型。因此,下列程式碼會擲回ClassCastException:String[] elements = (String[]) Arrays.asList("one", "two").toArray();在這種情況下,如要將
String保留在產生的陣列中做為元件型別,可以使用Collection.toArray(Object[]):String[] elements = Arrays.asList("two", "one").toArray(new String[0]);語言代碼處理方式異動:使用
LocaleAPI 時,希伯來文、意第緒文和印尼文的語言代碼不會再轉換為舊版代碼 (希伯來文:iw、意第緒文:ji、印尼文:in)。指定這些語言的語言代碼時,請改用 ISO 639-1 的代碼 (希伯來文:he、意第緒文:yi、印尼文:id)。隨機整數序列的變更:根據 https://bugs.openjdk.org/browse/JDK-8301574 中所做的變更,下列
Random.ints()方法現在會傳回與Random.nextInt()方法不同的數字序列:一般來說,這項變更不會導致應用程式發生中斷行為,但您的程式碼不應預期從
Random.ints()方法產生的序列會與Random.nextInt()相符。
在應用程式的建構設定中更新 compileSdk,以使用 Android 15 (API 級別 35) 後,新的 SequencedCollection API 可能會影響應用程式的相容性:
與
kotlin-stdlib中的MutableList.removeFirst()和MutableList.removeLast()擴充函式發生衝突Java 中的
List型別會對應至 Kotlin 中的MutableList型別。由於 Android 15 (API 級別 35) 已推出List.removeFirst()和List.removeLast()API,Kotlin 編譯器會將函式呼叫 (例如list.removeFirst()) 靜態解析為新的ListAPI,而不是kotlin-stdlib中的擴充功能函式。如果應用程式重新編譯時,將
compileSdk設為35,並將minSdk設為34以下,然後在 Android 14 以下版本執行,就會擲回執行階段錯誤:java.lang.NoSuchMethodError: No virtual method removeFirst()Ljava/lang/Object; in class Ljava/util/ArrayList;Android Gradle 外掛程式中的現有
NewApilint 選項可以偵測這些新 API 用法。./gradlew lintMainActivity.kt:41: Error: Call requires API level 35 (current min is 34): java.util.List#removeFirst [NewApi] list.removeFirst()如要修正執行階段例外狀況和 Lint 錯誤,請在 Kotlin 中將
removeFirst()和removeLast()函式呼叫分別替換為removeAt(0)和removeAt(list.lastIndex)。如果您使用 Android Studio Ladybug | 2024.1.3 以上版本,系統也會提供這些錯誤的快速修正選項。如果已停用 Lint 選項,請考慮移除
@SuppressLint("NewApi")和lintOptions { disable 'NewApi' }。與 Java 中的其他方法發生衝突
現有型別已新增方法,例如
List和Deque。這些新方法可能與其他介面和類別中,名稱和引數類型相同的方法不相容。如果方法簽章發生不相容的衝突,javac編譯器會輸出建構時間錯誤。例如:錯誤示例 1:
javac MyList.javaMyList.java:135: error: removeLast() in MyList cannot implement removeLast() in List public void removeLast() { ^ return type void is not compatible with Object where E is a type-variable: E extends Object declared in interface List錯誤示例 2:
javac MyList.javaMyList.java:7: error: types Deque<Object> and List<Object> are incompatible; public class MyList implements List<Object>, Deque<Object> { both define reversed(), but with unrelated return types 1 error錯誤示例 3:
javac MyList.javaMyList.java:43: error: types List<E#1> and MyInterface<E#2> are incompatible; public static class MyList implements List<Object>, MyInterface<Object> { class MyList inherits unrelated defaults for getFirst() from types List and MyInterface where E#1,E#2 are type-variables: E#1 extends Object declared in interface List E#2 extends Object declared in interface MyInterface 1 error如要修正這些建構錯誤,實作這些介面的類別應使用相容的回傳型別覆寫方法。例如:
@Override public Object getFirst() { return List.super.getFirst(); }
安全性
Android 15 包含多項變更,可提升系統安全性,協助保護應用程式和使用者免受惡意應用程式侵害。
受限的 TLS 版本
Android 15 限制了对 TLS 版本 1.0 和 1.1 的使用。这些版本之前已在 Android 中被弃用,但现在不允许面向 Android 15 的应用使用。
安全啟動背景活動
Android 15 protects users from malicious apps and gives them more control over their devices by adding changes that prevent malicious background apps from bringing other apps to the foreground, elevating their privileges, and abusing user interaction. Background activity launches have been restricted since Android 10 (API level 29).
Other changes
- Change
PendingIntentcreators to block background activity launches by default. This helps prevent apps from accidentally creating aPendingIntentthat could be abused by malicious actors. - Don't bring an app to the foreground unless the
PendingIntentsender allows it. This change aims to prevent malicious apps from abusing the ability to start activities in the background. By default, apps are not allowed to bring the task stack to the foreground unless the creator allows background activity launch privileges or the sender has background activity launch privileges. - Control how the top activity of a task stack can finish its task. If the top activity finishes a task, Android will go back to whichever task was last active. Moreover, if a non-top activity finishes its task, Android will go back to the home screen; it won't block the finish of this non-top activity.
- Prevent launching arbitrary activities from other apps into your own task. This change prevents malicious apps from phishing users by creating activities that appear to be from other apps.
- Block non-visible windows from being considered for background activity launches. This helps prevent malicious apps from abusing background activity launches to display unwanted or malicious content to users.
更安全的意圖
Android 15 introduces StrictMode for
intents.
In order to see detailed logs about Intent usage violations, use following
method:
Kotlin
fun onCreate() { StrictMode.setVmPolicy(VmPolicy.Builder() .detectUnsafeIntentLaunch() .build() ) }
Java
public void onCreate() { StrictMode.setVmPolicy(new VmPolicy.Builder() .detectUnsafeIntentLaunch() .build()); }
使用者體驗和系統 UI
Android 15 包含一些變更,旨在打造更一致、直覺的使用者體驗。
視窗插邊變更
There are two changes related to window insets in Android 15: edge-to-edge is enforced by default, and there are also configuration changes, such as the default configuration of system bars.
全面实施政策
如果应用以 Android 15(API 级别 35)为目标平台,则在搭载 Android 15 的设备上默认以无边框显示。
这是一项重大变更,可能会对应用的界面产生负面影响。这些更改会影响以下界面区域:
- 手势柄导航栏
- 默认透明。
- 底部偏移量处于停用状态,因此内容会绘制在系统导航栏后面,除非应用了边衬区。
setNavigationBarColor和R.attr#navigationBarColor已弃用,不会影响手势导航。setNavigationBarContrastEnforced和R.attr#navigationBarContrastEnforced继续对使用手势进行导航没有任何影响。
- “三按钮”导航
- 默认情况下,不透明度设置为 80%,颜色可能与窗口背景颜色一致。
- 底部偏移量已停用,因此内容会绘制在系统导航栏后面,除非应用了边衬区。
setNavigationBarColor和R.attr#navigationBarColor默认设置为与窗口背景保持一致。窗口背景必须是颜色可绘制对象,才能应用此默认值。此 API 已弃用,但仍会影响三按钮导航。setNavigationBarContrastEnforced和R.attr#navigationBarContrastEnforced默认值为 true,这会在三按钮导航栏中添加 80% 不透明度的背景。
- 状态栏
- 默认透明。
- 顶部偏移量处于停用状态,因此内容会绘制在状态栏后面,除非应用了边衬区。
setStatusBarColor和R.attr#statusBarColor已弃用,对 Android 15 没有影响。setStatusBarContrastEnforced和R.attr#statusBarContrastEnforced已废弃,但仍会对 Android 15 产生影响。
- 刘海屏
- 非浮动窗口的
layoutInDisplayCutoutMode必须为LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS。SHORT_EDGES、NEVER和DEFAULT会被解读为ALWAYS,这样用户就不会看到因刘海屏而产生的黑条,并且应用会显示在屏幕的整个边缘。
- 非浮动窗口的
以下示例展示了应用在以 Android 15(API 级别 35)为目标平台之前和之后,以及在应用边衬区之前和之后的效果。此示例并不全面,在 Android Auto 上可能会显示不同的内容。
如果应用已实现全屏显示,需要检查哪些方面
如果您的应用已实现全屏显示并应用边衬区,则基本上不会受到影响,但以下情形除外。不过,即使您认为自己不受影响,我们仍建议您测试应用。
- 您有一个非浮动窗口,例如使用
SHORT_EDGES、NEVER或DEFAULT而不是LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS的Activity。如果您的应用在启动时崩溃,这可能是由启动画面引起的。您可以将核心启动画面依赖项升级到 1.2.0-alpha01 或更高版本,也可以设置window.attributes.layoutInDisplayCutoutMode = WindowManager.LayoutInDisplayCutoutMode.always。 - 可能存在流量较低且界面被遮挡的屏幕。验证这些访问频率较低的界面是否没有被遮挡的界面。低流量屏幕包括:
- 初始配置或登录界面
- “设置”页面
如果您的应用尚未实现全屏显示,需要检查哪些方面
如果您的应用尚未实现全屏显示,则很可能会受到影响。除了已实现全屏显示的边缘到边缘应用的相关场景之外,您还应考虑以下事项:
- 如果您的应用在 Compose 中使用 Material 3 组件 (
androidx.compose.material3),例如TopAppBar、BottomAppBar和NavigationBar,这些组件很可能不会受到影响,因为它们会自动处理边衬区。 - 如果您的应用使用的是 Compose 中的 Material 2 组件 (
androidx.compose.material),这些组件不会自动处理边衬区。不过,您可以获得边衬区的访问权限,然后手动应用边衬区。在 androidx.compose.material 1.6.0 及更高版本中,使用windowInsets参数可为BottomAppBar、TopAppBar、BottomNavigation和NavigationRail手动应用边衬区。 同样,请为Scaffold使用contentWindowInsets参数。 - 如果应用使用了视图和 Material 组件 (
com.google.android.material),则大多数基于视图的 Material 组件(例如BottomNavigationView、BottomAppBar、NavigationRailView或NavigationView)都会处理边衬区,因此不需要执行额外的操作。不过,如果使用的是AppBarLayout,则需要添加android:fitsSystemWindows="true"。 - 对于自定义可组合项,请手动应用边衬区作为内边距。如果您的内容位于
Scaffold中,则可以使用Scaffold内边距值来使用边衬区。否则,请使用WindowInsets之一应用内边距。 - 如果应用使用的是视图和
BottomSheet、SideSheet或自定义容器,请使用ViewCompat.setOnApplyWindowInsetsListener应用内边距。对于RecyclerView,请使用此监听器应用内边距,同时添加clipToPadding="false"。
如果应用必须提供自定义后台保护,您需要检查哪些方面
如果您的应用必须为三按钮导航或状态栏提供自定义背景保护,则应使用 WindowInsets.Type#tappableElement() 获取三按钮导航栏高度或 WindowInsets.Type#statusBars,在系统栏后面放置一个可组合项或视图。
其他全屏显示资源
如需了解有关应用边衬区的其他注意事项,请参阅全屏视图和全屏 Compose 指南。
已弃用的 API
以下 API 已弃用,但未停用:
R.attr#enforceStatusBarContrastR.attr#navigationBarColor(适用于三按钮导航,alpha 为 80%)Window#isStatusBarContrastEnforcedWindow#setNavigationBarColor(适用于三按钮导航,alpha 为 80%)Window#setStatusBarContrastEnforced
以下 API 已弃用并停用:
R.attr#navigationBarColor(用于手势导航)R.attr#navigationBarDividerColorR.attr#statusBarColorWindow#setDecorFitsSystemWindowsWindow#getNavigationBarColorWindow#getNavigationBarDividerColorWindow#getStatusBarColorWindow#setNavigationBarColor(用于手势导航)Window#setNavigationBarDividerColorWindow#setStatusBarColor
稳定配置
如果應用程式指定 Android 15 (API 級別 35) 以上版本,Configuration系統不會再排除系統資訊列。如果您在 Configuration 類別中使用螢幕大小進行版面配置計算,請視需求改用適當的 ViewGroup、WindowInsets 或 WindowMetricsCalculator 等更合適的替代方案。
Configuration 自 API 1 起就已推出,這項資訊通常來自 Activity.onConfigurationChanged。這項資訊包括視窗密度、方向和大小。從 Configuration 傳回的視窗大小有一項重要特徵,就是先前會排除系統資訊列。
設定大小通常用於資源選取 (例如 /res/layout-h500dp),這仍是有效的用途。不過,我們一直不建議使用它來計算版面配置。如果這樣做,請立即遠離該裝置。視用途而定,您應將 Configuration 替換為更合適的項目。
如果使用它來計算版面配置,請使用適當的 ViewGroup,例如 CoordinatorLayout 或 ConstraintLayout。如要使用它判斷系統導覽列的高度,請使用 WindowInsets。如要瞭解應用程式視窗的目前大小,請使用 computeCurrentWindowMetrics。
下列清單說明這項異動會影響的欄位:
Configuration.screenWidthDp和screenHeightDp大小不再排除系統資訊列。Configuration.smallestScreenWidthDp會間接受到screenWidthDp和screenHeightDp變更的影響。Configuration.orientation會間接受到接近正方形裝置上screenWidthDp和screenHeightDp變更的影響。Display.getSize(Point)間接受到Configuration變更的影響。從 API 級別 30 開始,這項屬性已遭淘汰。- 自 API 級別 33 起,
Display.getMetrics()就已採用這種運作方式。
elegantTextHeight 屬性預設為 true
For apps targeting Android 15 (API level 35), the
elegantTextHeight TextView attribute
becomes true by default, replacing the compact font used by default with some
scripts that have large vertical metrics with one that is much more readable.
The compact font was introduced to prevent breaking layouts; Android 13 (API
level 33) prevents many of these breakages by allowing the text layout to
stretch the vertical height utilizing the fallbackLineSpacing
attribute.
In Android 15, the compact font still remains in the system, so your app can set
elegantTextHeight to false to get the same behavior as before, but it is
unlikely to be supported in upcoming releases. So, if your app supports the
following scripts: Arabic, Lao, Myanmar, Tamil, Gujarati, Kannada, Malayalam,
Odia, Telugu or Thai, test your app by setting elegantTextHeight to true.
elegantTextHeight behavior for apps targeting Android 14 (API level 34) and lower.
elegantTextHeight behavior for apps targeting Android 15.複雜字母形狀的 TextView 寬度變化
在以前的 Android 版本中,某些具有复杂形状的手写字体或语言可能会在上一个或下一个字符的区域绘制字母。在某些情况下,此类字母会在开头或结尾处被剪裁。从 Android 15 开始,TextView 会分配宽度,以便为此类字母绘制足够的空间,并允许应用请求向左额外添加内边距以防止剪裁。
由于此更改会影响 TextView 确定宽度的方式,因此如果应用以 Android 15(API 级别 35)或更高版本为目标平台,TextView 会默认分配更多宽度。您可以通过对 TextView 调用 setUseBoundsForWidth API 来启用或停用此行为。
由于添加左内边距可能会导致现有布局未对齐,因此默认情况下不会添加内边距,即使以 Android 15 或更高版本为目标平台的应用也是如此。不过,您可以通过调用 setShiftDrawingOffsetForStartOverhang 添加额外的内边距以防止剪裁。
以下示例展示了这些更改如何改进某些字体和语言的文本布局。
<TextView android:fontFamily="cursive" android:text="java" />
<TextView android:fontFamily="cursive" android:text="java" android:useBoundsForWidth="true" android:shiftDrawingOffsetForStartOverhang="true" />
<TextView android:text="คอมพิวเตอร์" />
<TextView android:text="คอมพิวเตอร์" android:useBoundsForWidth="true" android:shiftDrawingOffsetForStartOverhang="true" />
EditText 的預設行高會因語言代碼而異
In previous versions of Android, the text layout stretched the height of the
text to meet the line height of the font that matched the current locale. For
example, if the content was in Japanese, because the line height of the Japanese
font is slightly larger than the one of a Latin font, the height of the text
became slightly larger. However, despite these differences in line heights, the
EditText element was sized uniformly, regardless
of the locale being used, as illustrated in the following image:
EditText elements that
can contain text from English (en), Japanese (ja), and Burmese (my). The
height of the EditText is the same, even though these languages
have different line heights from each other.For apps targeting Android 15 (API level 35), a minimum line height is now
reserved for EditText to match the reference font for the specified Locale, as
shown in the following image:
EditText elements that
can contain text from English (en), Japanese (ja), and Burmese (my). The
height of the EditText now includes space to accommodate the
default line height for these languages' fonts.If needed, your app can restore the previous behavior by specifying the
useLocalePreferredLineHeightForMinimum attribute
to false, and your app can set custom minimum vertical metrics using the
setMinimumFontMetrics API in Kotlin and Java.
相機和媒體
Android 15 對指定 Android 15 以上版本的應用程式,進行了下列攝影機和媒體行為變更。
要求音訊焦點的限制
Apps that target Android 15 (API level 35) must be the top app or running a
foreground service in order to request audio focus. If an app
attempts to request focus when it does not meet one of these requirements, the
call returns AUDIOFOCUS_REQUEST_FAILED.
You can learn more about audio focus at Manage audio focus.
更新非 SDK 限制
基於與 Android 開發人員合作及最新的內部測試,Android 15 包含更新後的受限制非 SDK 介面清單。在限制非 SDK 介面之前,我們盡可能確保公開替代方案的可得性。
如果您的應用程式並不是以 Android 15 為目標版本,則此處所述的某些變更可能不會立即對您造成影響。不過,雖然應用程式視目標 API 級別而定,可以存取某些非 SDK 介面,但使用任何非 SDK 方法或欄位時,應用程式停止運作的風險極高。
如果不確定應用程式是否使用非 SDK 介面,可測試應用程式以便確認。如果應用程式仰賴非 SDK 介面,則應開始規劃遷移至 SDK 替代方案。不過我們瞭解,有些應用程式可使用非 SDK 介面運作。如果您除了為應用程式中的某個功能使用非 SDK 介面外,已別無他法,則應要求新的公用 API。
To learn more about the changes in this release of Android, see Updates to non-SDK interface restrictions in Android 15. To learn more about non-SDK interfaces generally, see Restrictions on non-SDK interfaces.