Android-如何通过用户名以编程方式切换用户? [英] Android - How to programmatically switch users by user name?

查看:74
本文介绍了Android-如何通过用户名以编程方式切换用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android的新手,我必须在工作中执行一个应用程序来执行以下任务:

I'm new to Android and I have to do an application at work that performs the following task:

  1. 我正在听一个应该向我发送用户名的套接字.
  2. 收到用户名后,我需要在平板电脑上切换到该用户帐户.
  3. 如果我收到与当前活动用户相同的用户名,则无需切换(显然).

我们的平板电脑植根并运行了带有Android 5.1.1 Lollipop的CyanogenMod 12.1.由于这不是要从Play商店下载的商业版本,因此我可以假定将授予我可能需要的所有权限.

Our tablets are rooted and running CyanogenMod 12.1 with Android 5.1.1 Lollipop. Since this is not a commercial version that will be downloaded from the Play Store I can assume that all permissions I may need will be granted.

我一直在寻找一种干净的代码方式来执行此操作,而无需运行shell命令或其他黑客程序,但找不到任何东西...

I searched for a clean code way to do that without running shell commands or other hacks but couldn't find anything...

推荐答案

我自己没有尝试过,但是您应该能够在设备上获得所有UserHandle设置,然后使用

Haven't tried it myself but you should be able to get all UserHandle setup on the device and then use DevicePolicyManager switchUser() method.

UserManager um = (UserManager) getSystemService(USER_SERVICE);
List<UserHandle> userProfiles = um.getUserProfiles();

为此,您必须将您的应用设置为设备管理员(因为您说它是内部完全托管的应用程序,所以应该不会有问题)

For this to work, you have to set your app as a device administrator (should not be a problem as you say it's an internal fully managed app)

这篇关于Android-如何通过用户名以编程方式切换用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆