Android - Get list of users and profile name

故事扮演 提交于 2019-12-24 01:54:11

问题


We can create several users and profiles on an Android devices, for example, there is a Main User, and I also set a Work Profile with name "Work Profile".

Using the following adb command:

adb shell pm list users

I'm getting this result:

UserInfo{0:My User:13} running UserInfo{10:Work Profile:30} running

Good, I can see my User Info with the name Work Profile on it. Now, I want to get this list from my application.

I tried to use this code:

for(Account account: AccountManager.get(this).getAccounts()) {
        DebugLog.e("COUCOU " + account.name + " " + account.toString());
    }

But the Work Profile account is not appearing in the list. Only the main user... Any idea how I could get the same list as the ADB shell command??


回答1:


If you have the root access to your device, or you are able to build your app as a system privileged application, you can use Privileged Permission Whitelisting to obtain the android.permission.MANAGE_USERS permission (which also requires you to sign your app with the platform signature). Then, you can get a list of all UserInfos by calling UserManager.getUsers() using Java reflection.




回答2:


You are confusing users with accounts. What you are using will only get you the accounts of the current user.

A device can have multiple users, which are the equivalent of users on a computer.

A user can have multiple accounts, i.e., let that be Gmail, Facebook, Twitter, etc. The accounts may be shared among users or not.

For more info on that topic, check out the official page on Supporting Multiple Users.

You can handle users with UserManager and you can get the list of the profiles of the current user using getUserProfiles, but what you'll get is a UserHandle (which you can use to get the userId), how to get the list of all users and the name is something I haven't found out yet, but on the worst case you can use the shell command pm, as shown in this post about Multiple user accounts on Android.



来源:https://stackoverflow.com/questions/37495126/android-get-list-of-users-and-profile-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!