当前位置: 首页 > news >正文

【笔记】Android 多用户模式和用户类型

简介

用户界面:System =》Multiple Users =》 开关多用户模式。

一般是不同用户模式下,有修改Settings应用配置的权限差异,因此需要通过用户类型对功能进行判断限制。

代码

通过UserManager可以获取当前用户的信息。

frameworks/base/core/java/android/os/UserManager.java

提供多种判断当前用户类型的接口

UserManager功能接口
APIComment
getUserType()

获取当前用户类型

@return the user type of the context user.

getUserName()

获取当前用户名

Returns the user name of the context user. This call is only available to applications on the system image.

isSystemUser()

判断是否为系统用户

Used to check if the context user is the system user. The system user is the initial user that is implicitly created on first boot and hosts most of the system services.

isGuestUser()

基于上下文,判断是否为访客用户

Used to check if the context user is a guest user. A guest user may be transient.

@return whether the context user is a guest user.

isGuestUser(@UserIdInt int userId)

判断指定ID是否为访客用户

Checks if a user is a guest user.

@return whether user is a guest user.

isUserAdmin(@UserIdInt int userId)

判断指定指定id的用户是否为admin(admin可以不唯一)

返回user.isAdmin()

@hide Returns whether the provided user is an admin user. There can be more than one admin user.

源码

/*** Manages users and user details on a multi-user system. There are two major categories of* users: fully customizable users with their own login, and profiles that share a workspace* with a related user.* <p>* Users are different from accounts, which are managed by* {@link AccountManager}. Each user can have their own set of accounts.* <p>* See {@link DevicePolicyManager#ACTION_PROVISION_MANAGED_PROFILE} for more on managed profiles.*/
@SystemService(Context.USER_SERVICE)
@android.ravenwood.annotation.RavenwoodKeepPartialClass
public class UserManager {/*** @return the user type of the context user.* @hide*/@TestApi@RequiresPermission(anyOf = {android.Manifest.permission.MANAGE_USERS,android.Manifest.permission.CREATE_USERS,android.Manifest.permission.QUERY_USERS})@UserHandleAwarepublic @NonNull String getUserType() {UserInfo userInfo = getUserInfo(mUserId);return userInfo == null ? "" : userInfo.userType;}

获取用户信息的方法

代码案例:

通过系统服务获取UserManager对象,然后根据需求get信息。

   //判断是否为Owner机主  private static boolean isAdminUser(Context context) {if (context == null) return false;final UserManager userManager = context.getSystemService(UserManager.class);if (userManager == null) return false;//获取当前用户类型 Log.d(TAG, "isAdminUser: Now user is " + userManager.getUserType());return userManager.isAdminUser();}

常见用户类型

常见类型是Owner,User和Guest。

多用户模式用户类型映射关系
StringUSER_TYPE用户场景说明
USER_TYPE_FULL_SYSTEMandroid.os.usertype.full.SYSTEM

Owner,即机主,adminUser。

 User type representing a {@link UserHandle#USER_SYSTEM system} user that is a human user.
 This type of user cannot be created; it can only pre-exist on first boot.
USER_TYPE_FULL_SECONDARYandroid.os.usertype.full.SECONDARYUser,非Owner(机主)用户。User type representing a regular non-profile non-{@link UserHandle#USER_SYSTEM system} human
user.
This is sometimes called an ordinary 'secondary user'.
USER_TYPE_FULL_GUESTandroid.os.usertype.full.GUESTGuset,访客模式User type representing a guest user that may be transient.
USER_TYPE_FULL_DEMOandroid.os.usertype.full.DEMO怎么变成demo?User type representing a user for demo purposes only, which can be removed at any time.
USER_TYPE_FULL_RESTRICTEDandroid.os.usertype.full.RESTRICTED受限用户,profile是什么?User type representing a "restricted profile" user, which is a full user that is subject to
certain restrictions from a parent user. Note, however, that it is NOT technically a profile.
USER_TYPE_PROFILE_MANAGEDandroid.os.usertype.profile.MANAGEDDPC是啥?APN里面有查询

User type representing a managed profile, which is a profile that is to be managed by a
device policy controller (DPC).
The intended purpose is for work profiles, which are managed by a corporate entity.

 @FlaggedApi(android.os.Flags.FLAG_ALLOW_PRIVATE_PROFILE)

USER_TYPE_PROFILE_CLONEandroid.os.usertype.profile.CLONE克隆某用户

User type representing a clone profile. Clone profile is a user profile type used to run
second instance of an otherwise single user App (eg, messengers). Currently only the
{@link android.content.pm.UserInfo#isMain()} user can have a clone profile.

@FlaggedApi(android.os.Flags.FLAG_ALLOW_PRIVATE_PROFILE)

USER_TYPE_PROFILE_PRIVATEandroid.os.usertype.profile.PRIVATE

User type representing a private profile. Private profile is a user profile that can be used
as an alternative user-space to install and use sensitive apps.
UI surfaces can adopt an alternative strategy to show apps belonging to this profile, in line
with their sensitive nature.
 

@FlaggedApi(android.os.Flags.FLAG_ALLOW_PRIVATE_PROFILE)

USER_TYPE_PROFILE_TESTandroid.os.usertype.profile.TEST测试User type representing a generic profile for testing purposes. Only on debuggable builds.
USER_TYPE_PROFILE_COMMUNALandroid.os.usertype.profile.COMMUNAL多个用户共享一些资源而不共享敏感信息。User type representing a communal profile, which is shared by all users of the device.
public static final String USER_TYPE_FULL_SYSTEM = "android.os.usertype.full.SYSTEM";
public static final String USER_TYPE_FULL_SECONDARY = "android.os.usertype.full.SECONDARY";
public static final String USER_TYPE_FULL_GUEST = "android.os.usertype.full.GUEST";
public static final String USER_TYPE_FULL_DEMO = "android.os.usertype.full.DEMO";
public static final String USER_TYPE_FULL_RESTRICTED = "android.os.usertype.full.RESTRICTED";
public static final String USER_TYPE_PROFILE_MANAGED = "android.os.usertype.profile.MANAGED";
public static final String USER_TYPE_PROFILE_CLONE = "android.os.usertype.profile.CLONE";
public static final String USER_TYPE_PROFILE_PRIVATE = "android.os.usertype.profile.PRIVATE";
public static final String USER_TYPE_PROFILE_TEST = "android.os.usertype.profile.TEST";
public static final String USER_TYPE_PROFILE_COMMUNAL = "android.os.usertype.profile.COMMUNAL";
public static final String USER_TYPE_FULL_SYSTEM = "android.os.usertype.full.SYSTEM";
public static final String USER_TYPE_FULL_SECONDARY = "android.os.usertype.full.SECONDARY";
public static final String USER_TYPE_FULL_GUEST = "android.os.usertype.full.GUEST";
public static final String USER_TYPE_FULL_DEMO = "android.os.usertype.full.DEMO";
public static final String USER_TYPE_FULL_RESTRICTED = "android.os.usertype.full.RESTRICTED";
public static final String USER_TYPE_PROFILE_MANAGED = "android.os.usertype.profile.MANAGED";
public static final String USER_TYPE_PROFILE_CLONE = "android.os.usertype.profile.CLONE";
public static final String USER_TYPE_PROFILE_PRIVATE = "android.os.usertype.profile.PRIVATE";
public static final String USER_TYPE_PROFILE_TEST = "android.os.usertype.profile.TEST";
public static final String USER_TYPE_PROFILE_COMMUNAL = "android.os.usertype.profile.COMMUNAL";
public static final String USER_TYPE_SYSTEM_HEADLESS = "android.os.usertype.system.HEADLESS";

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Codeforces Round 965 (Div. 2)
  • 如何对 GitLab 中文版进行升级?
  • 鸿蒙内核源码分析(进程管理篇) | 谁在管理内核资源?
  • cpu管理
  • Oracle(63)什么是临时表(Temporary Table)?
  • Dubbo,Zookeeper,NSF,Druid,CouchDB未授权访问漏洞(附带修复方法)
  • GORM 插入和批量插入操作介绍
  • EmguCV学习笔记 VB.Net 2.S 特别示例
  • 系统运维工程师学习路线
  • 如何有效利用渗压计来避免溃坝风险
  • 【YashanDB知识库】生成迁移报告失败,“报错未知类型错误异常:“
  • iOS的App启动详细过程(底层知识)
  • python爬取豆瓣电影数据
  • Dynamics 365 如何查看某个自定义实体是谁创建的
  • python之numpy(3 矩阵属性及矩阵运算)
  • 【MySQL经典案例分析】 Waiting for table metadata lock
  • Angular js 常用指令ng-if、ng-class、ng-option、ng-value、ng-click是如何使用的?
  • GitUp, 你不可错过的秀外慧中的git工具
  • HTTP中的ETag在移动客户端的应用
  • input实现文字超出省略号功能
  • Travix是如何部署应用程序到Kubernetes上的
  • 回顾2016
  • 模仿 Go Sort 排序接口实现的自定义排序
  • 前端学习笔记之观察者模式
  • 收藏好这篇,别再只说“数据劫持”了
  • 限制Java线程池运行线程以及等待线程数量的策略
  • 扩展资源服务器解决oauth2 性能瓶颈
  • 组复制官方翻译九、Group Replication Technical Details
  • #QT(TCP网络编程-服务端)
  • #数据结构 笔记一
  • (1)Map集合 (2)异常机制 (3)File类 (4)I/O流
  • (html5)在移动端input输入搜索项后 输入法下面为什么不想百度那样出现前往? 而我的出现的是换行...
  • (带教程)商业版SEO关键词按天计费系统:关键词排名优化、代理服务、手机自适应及搭建教程
  • (第61天)多租户架构(CDB/PDB)
  • (二)十分简易快速 自己训练样本 opencv级联lbp分类器 车牌识别
  • (附源码)计算机毕业设计SSM在线影视购票系统
  • (算法)N皇后问题
  • (已解决)Bootstrap精美弹出框模态框modal,实现js向modal传递数据
  • (转)es进行聚合操作时提示Fielddata is disabled on text fields by default
  • (转)项目管理杂谈-我所期望的新人
  • .net SqlSugarHelper
  • .NET 读取 JSON格式的数据
  • .Net 应用中使用dot trace进行性能诊断
  • .NET 中让 Task 支持带超时的异步等待
  • .NET/C# 项目如何优雅地设置条件编译符号?
  • .NetCore Flurl.Http 升级到4.0后 https 无法建立SSL连接
  • .Net程序帮助文档制作
  • .NET框架设计—常被忽视的C#设计技巧
  • .stream().map与.stream().flatMap的使用
  • // an array of int
  • @reference注解_Dubbo配置参考手册之dubbo:reference
  • [].slice.call()将类数组转化为真正的数组
  • [2023-年度总结]凡是过往,皆为序章
  • [23] 4K4D: Real-Time 4D View Synthesis at 4K Resolution
  • [AIGC] SpringBoot的自动配置解析