首页 > App

安卓13新功能和API

2023-12-15 浏览: 46

对于当前未使用自定义应用内语言选择器的应用,无需执行任何其他操作。

对于具有或想要使用应用内语言选择器的应用,请使用这些新 API(而非应用的自定义逻辑)来处理相关设置和获取用户对应用的首选语言设置。

使用 AndroidX 支持库来实现

为了向后兼容以前的 Android 版本,建议使用 AndroidX 支持库来实现应用内语言选择器。使用Appcompat 1.6.0-alpha01或更高版本中提供的setApplicationLocales()方法。

例如,如需设置用户的首选语言,您需要让用户在语言选择器中选择语言区域,然后在系统中设置该值:

LocaleListCompat appLocale = LocaleListCompat.forLanguageTags("xx-YY");

// Call this on the main thread as it may require Activity.restart()

AppCompatDelegate.setApplicationLocales(appLocale);

使用 Android 框架 API 来实现

您还可以通过setApplicationLocales()和getApplicationLocales()方法,使用 Android 框架 API 来实现应用内语言选择器。

例如,如需设置用户的首选语言,您需要让用户在语言选择器中选择语言区域,然后在系统中设置该值:

// 1. Inside an activity, in-app language picker gets an input locale "xx-YY"

// 2. App calls the API to set its localem

Context.getSystemService(LocaleManager.class).setApplicationLocales(newLocaleList(Locale.forLanguageTag("xx-YY")));

// 3. The system updates the locale and restarts the app, including any configuration updates

// 4. The app is now displayed in "xx-YY" language

如需获取用户当前的首选语言以显示在语言选择器中,您的应用可以从系统中取回该值:

// 1. App calls the API to get the preferred locale

LocaleList currentAppLocales = mContext.getSystemService(LocaleManager.class).getApplicationLocales();

// 2. App uses the returned LocaleList to display languages to the user

标签: 安卓13新功能和API