Android - How to programmatically switch users by user name?

后端 未结 2 1449
轮回少年
轮回少年 2021-01-14 15:04

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

  1. I listen to a socket that is supposed to send me a user name.
相关标签:
2条回答
  • 2021-01-14 15:30

    Did you manage to get the user names once you become the device owner ? getUserProfiles and getSecondaryUsers can be used, but they both return UserHandle. I can't get the user name registered at creation. It seems that the only way is to add the MANAGE_USERS permission to the manifest but that supposes the app to be a system app : https://developer.android.com/reference/android/os/UserManager#getUserName()

    I think I will create a persistent list somewhere.

    0 讨论(0)
  • 2021-01-14 15:35

    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)

    0 讨论(0)
提交回复
热议问题