自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
实现自定义用户类型
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Android 11 引入了定义明确的用户类型概念,用于表示 Android 多用户功能所允许的各种不同类型的用户。借助此功能,OEM 可以自定义预定义的 AOSP 用户类型,并定义新的个人资料类型。如需了解详情,请参阅有关用户类型的部分。
本页详细介绍了自定义用户类型所需的实现准则。
自定义
为了自定义 AOSP 用户类型并定义新的个人资料类型,OEM 必须使用首选的自定义设置叠加 config_user_types.xml
。config_user_types.xml
文件包含参考实现和完整的可配置属性列表。
在 config_user_types.xml
文件中指定的任何属性(如 default-restrictions
)都会覆盖 AOSP 默认值。任何未指定的属性都遵循 AOSP 默认设置。更改大多数属性(例如个人资料类型的标志属性)都会影响该用户类型的现有用户。但是,由于 default-restrictions
仅在创建用户时应用,因此如果 config_user_types.xml
文件通过 OTA 进行更改,则修改此特定属性不会影响现有用户。同样,仅在创建新用户时指定最大用户数;不会移除现有用户。
各类用户当前的自定义限制如下:
- 您可以对个人资料进行完全自定义和定义。在这种情况下,OEM 负责根据需要进行平台修改,以确保 Android 支持其自定义个人资料,因为 AOSP 仅支持预定义的 AOSP 用户类型。
- 不能指定完整权限用户,只能自定义其
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>
在此代码示例中,以下 AOSP 用户类型通过修改支持的属性进行自定义:
如需了解这些属性的含义和默认值,请参阅 UserTypeFactory.java
和 UserTypeDetails.java
。
定义自定义个人资料类型
以下代码示例展示了如何定义新的自定义个人资料类型:
<user-types version="1">
<profile-type
name="com.example.profilename"
max-allowed-per-parent="2" />
<change-user-type
from="android.os.usertype.profile.MANAGED"
to="com.example.profilename"
whenVersionLeq="1" />
</user-types>
在此代码示例中,com.example.profilename
个人资料类型的定义如下:
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-03-26。
[null,null,["最后更新时间 (UTC):2025-03-26。"],[],[],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."]]