自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
實作自訂使用者類型
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Android 11 引入了明確定義的使用者類型概念,代表 Android 多使用者功能允許的所有不同類型使用者。有了這項功能,原始設備製造商 (OEM) 就能自訂預先定義的 AOSP 使用者類型,並定義新的設定檔類型。詳情請參閱「使用者類型」一節。
本頁面詳細說明自訂使用者類型所需的導入指南。
自訂
為了自訂 AOSP 使用者類型並定義新的設定檔類型,原始設備製造商必須將 config_user_types.xml
與偏好的自訂項目重疊。config_user_types.xml
檔案包含參考實作項目,以及可設定屬性的完整清單。
config_user_types.xml
檔案中指定的任何屬性 (例如 default-restrictions
) 都會覆寫 Android 開放原始碼計畫的預設值。未指定的任何屬性都會遵循 AOSP 預設值。變更大部分屬性 (例如個人資料類型的徽章屬性) 會影響該使用者類型的現有使用者。不過,由於 default-restrictions
只會在建立使用者時套用,因此如果 OTA 變更 config_user_types.xml
檔案,修改這個特定屬性對現有使用者不會有任何影響。同樣地,指定使用者人數上限只會在建立新使用者時生效,不會移除現有使用者。
目前各使用者類型的自訂限制如下:
- 您可以完全自訂和定義設定檔。在這種情況下,由於 AOSP 僅支援預先定義的 AOSP 使用者類型,因此原始設備製造商 (OEM) 必須負責依需求修改平台,讓自訂設定檔在 Android 中獲得支援。
- 無法定義完整使用者,只能自訂其
default-restrictions
屬性。
- 系統使用者無法透過這項機制自訂。在這種情況下,您可以使用
com.android.internal.R.array.config_defaultFirstUserRestrictions
設定 default-restrictions
。詳情請參閱 config.xml
。
修改現有的使用者類型
您可以覆寫現有使用者類型的屬性,如以下程式碼範例所示,藉此自訂現有使用者類型:
<user-types version="0">
<full-type name="android.os.usertype.full.SECONDARY" >
<default-restrictions no_sms="true" />
</full-type>
<profile-type
name='android.os.usertype.profile.MANAGED'
max-allowed-per-parent='2'
icon-badge='@android:drawable/ic_corp_icon_badge_case'
badge-plain='@android:drawable/ic_corp_badge_case'
badge-no-background='@android:drawable/ic_corp_badge_no_background' >
<badge-labels>
<item res='@android:string/managed_profile_label_badge' />
<item res='@android:string/managed_profile_label_badge_2' />
</badge-labels>
<badge-colors>
<item res='@android:color/profile_badge_1' />
<item res='@android:color/profile_badge_2' />
</badge-colors>
<default-restrictions no_sms="true" no_outgoing_calls="true" />
</profile-type>
</user-types>
在這個程式碼範例中,我們會透過修改支援的屬性,自訂下列 Android 開放原始碼計畫 (AOSP) 使用者類型:
如要瞭解這些屬性的含義和預設值,請參閱 UserTypeFactory.java
和 UserTypeDetails.java
。
定義自訂設定檔類型
以下程式碼範例說明如何定義新的自訂設定檔類型:
<user-types version=&qu>ot;1&<quot;
profile-type
name="com.example.profilename"
max-allo>wed-pe<r-parent="2" /
change-user-type
from="android.os.usertype.profile.MANAGED"
to="com.exam>p<le.profilen>a
me"
whenVersionLeq="1" /
/user-types
在這個程式碼範例中,com.example.profilename
設定檔類型定義如下:
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-08-30 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-30 (世界標準時間)。"],[],[],null,["Android 11 has introduced the concept of well-defined\nuser types, representing all the different types of users allowed by the\nAndroid Multi-user feature. With this feature, OEMs can customize predefined\nAOSP user types and define new profile types. See the section on\n[user types](/docs/devices/admin/multi-user#user_types) for more information.\n\nThis page details the implementation guidelines needed to customize user types.\n\nCustomization\n\nIn order to customize AOSP user types and to define new profile types, the OEM\nmust overlay\n[`config_user_types.xml`](https://cs.android.com/android/platform/superproject/+/android-latest-release:frameworks/base/core/res/res/xml/config_user_types.xml?q=config_user_types.xml)\nwith the preferred customizations. The `config_user_types.xml` file\ncontains a reference implementation and a comprehensive list of configurable\nattributes.\n\nAny attribute, such as `default-restrictions`, that is specified in the\n`config_user_types.xml` file, overwrites the AOSP default. Any attribute that is\nnot specified, obeys the AOSP default. Changing most attributes, such as a\nprofile type's badge attributes, affects pre-existing users of that user type.\nHowever, because `default-restrictions` are only applied at the time a user is\ncreated, modifying this particular attribute, in the event the\n`config_user_types.xml` file is changed by OTA, has no effect on pre-existing\nusers. Similarly, specifying the maximum number of users only applies when\ncreating new users; existing users aren't removed.\n\nCurrent customization restrictions for each user type are as follows:\n\n- Profiles can be fully customized and defined. In this case, the OEM is responsible for making platform modifications as required for their custom profile to be supported in Android, since AOSP only supports the predefined AOSP user-types.\n- Full users cannot be defined and only their `default-restrictions` attribute can be customized.\n- The system user cannot be customized using this mechanism. In this case, `default-restrictions` can be set using `com.android.internal.R.array.config_defaultFirstUserRestrictions`. See [`config.xml`](https://cs.android.com/android/platform/superproject/+/android-latest-release:frameworks/base/core/res/res/values/config.xml?q=config_defaultFirstUserRestrictions) for more information.\n\nModify existing user types\n\nExisting user types can be customized by overriding their attributes as shown\nin the following code sample: \n\n \u003cuser-types version=\"0\"\u003e\n \u003cfull-type name=\"android.os.usertype.full.SECONDARY\" \u003e\n \u003cdefault-restrictions no_sms=\"true\" /\u003e\n \u003c/full-type\u003e\n\n \u003cprofile-type\n name='android.os.usertype.profile.MANAGED'\n max-allowed-per-parent='2'\n icon-badge='@android:drawable/ic_corp_icon_badge_case'\n badge-plain='@android:drawable/ic_corp_badge_case'\n badge-no-background='@android:drawable/ic_corp_badge_no_background' \u003e\n \u003cbadge-labels\u003e\n \u003citem res='@android:string/managed_profile_label_badge' /\u003e\n \u003citem res='@android:string/managed_profile_label_badge_2' /\u003e\n \u003c/badge-labels\u003e\n \u003cbadge-colors\u003e\n \u003citem res='@android:color/profile_badge_1' /\u003e\n \u003citem res='@android:color/profile_badge_2' /\u003e\n \u003c/badge-colors\u003e\n \u003cdefault-restrictions no_sms=\"true\" no_outgoing_calls=\"true\" /\u003e\n \u003c/profile-type\u003e\n \u003c/user-types\u003e\n\nIn this code sample, the following AOSP user types are customized by modifying\nthe supported properties:\n\n- Full user `android.os.usertype.full.SECONDARY`:\n\n - The default restriction of `no_sms` is set to true by specifying `default-restrictions no_sms=\"true\"`.\n- Profile user `android.os.usertype.profile.MANAGED`:\n\n - Two profiles are allowed for each parent user by setting `max-allowed-per-parent='2'`.\n - Badge attributes are set to chosen values using `icon-badge`, `badge-plain`, `badge-no-background`, `badge-labels`, `badge-colors`.\n - The default restrictions of `no_sms` and `no_outgoing_calls` are set to true by specifying `default-restrictions no_sms=\"true\" no_outgoing_calls=\"true\"`.\n\nRefer to [`UserTypeFactory.java`](https://cs.android.com/android/platform/superproject/+/android-latest-release:frameworks/base/services/core/java/com/android/server/pm/UserTypeFactory.java;drc=2b4306a8ecd64f6e03498104f314600f9cc7507c;l=77) and [`UserTypeDetails.java`](https://cs.android.com/android/platform/superproject/+/android-latest-release:frameworks/base/services/core/java/com/android/server/pm/UserTypeDetails.java;drc=78a66d6c36451d8c91b2473e3b4ac8bafbd79b41;l=59)\nfor the meaning and default values of these properties.\n\nDefine custom profile types\n\nThe following code sample shows how new, custom profile types are defined: \n\n \u003cuser-types version=&qu\u003eot;1&\u003cquot;\n profile-type\n name=\"com.example.profilename\"\n max-allo\u003ewed-pe\u003cr-parent=\"2\" /\n\n change-user-type\n from=\"android.os.usertype.profile.MANAGED\"\n to=\"com.exam\u003ep\u003cle.profilen\u003eame\"\n whenVersionLeq=\"1\" /\n /user-types\n\nIn this code sample, the `com.example.profilename` profile type is\ndefined as follows:\n\n- `max-allowed-per-parents` is set to `2` for two profiles per parent user.\n\n- `change-user-type`: converts *all* existing managed profiles of the type\n `android.os.usertype.profile.MANAGED` to the new `com.example.profilename`\n type when upgrading the device from a `user-type` version of `\u003c= 1`\n through OTA."]]