5

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. After receiving the user name, I need to switch to that user account on a tablet.
  3. If I receive the same user name as the currently active user I don't need to switch (obviously).

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.

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

Liran Friedman
  • 4,027
  • 13
  • 53
  • 96
  • I think your question is a little fage. When referring to "user" and switching it, are you talking about: 1. Android "multi-user" API and how to use it? ( https://source.android.com/devices/tech/admin/multi-user.html). 2. Some custom server data that represents your user and how to implement it? – EladB Feb 27 '17 at 17:36
  • I mean the user that you can add via the Android settings menu. Where you have the Owner(You) and you can add more users to your device. – Liran Friedman Feb 27 '17 at 17:53
  • Maybe it's called a system user ? – Liran Friedman Feb 27 '17 at 17:56

2 Answers2

1

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)

Idan
  • 2,819
  • 4
  • 33
  • 61
  • Currently, `um.getUserProfiles();` always return 1 item. Could it be because I need to set my app as a device admin? – Liran Friedman Feb 28 '17 at 08:32
  • Possibly... Start with setting up the app as administrator and go from there as this is a MUST step. There is another option to get all users on the device if you are rooted and that's looking at the FS (wouldn't be my first choice). You can see an example here: http://stackoverflow.com/a/32140247/742595 – Idan Feb 28 '17 at 08:51
  • I'm now following the instruction on the links you sent. First to set device admin and after that I will try to get users and switch user. I will update a comment here to let you know how it went (will do my best to do it in a hurry). – Liran Friedman Feb 28 '17 at 08:54
  • The code samples on how to user [set my app as a device admin](https://developer.android.com/guide/topics/admin/device-admin.html) are lacking good a lot of code to understand how to implement it. Can you please post a working code sample to help a first timer understand :-) ? – Liran Friedman Mar 01 '17 at 09:25
  • I think I got the device admin, to check it I need to be able to switch user but to do so I need to be able to get a user by his name. How can I do that ? I have a list of user names and I need to be able to switch to each of them by selecting a name. – Liran Friedman Mar 01 '17 at 09:50
  • Doesn't getUserProfiles() returned all users handles? – Idan Mar 01 '17 at 10:09
  • It always returns 1 handle – Liran Friedman Mar 01 '17 at 10:10
  • And when I'm trying to switch user I'm getting this error: `java.lang.SecurityException: Admin ComponentInfo{packagename/packagename.DeviceAdmin} does not own the device`. But in settings -> security -> device administrators I DO see my application, so it does have admin persmission... – Liran Friedman Mar 01 '17 at 10:18
  • Try this one: http://stackoverflow.com/questions/21183328/how-to-make-my-app-a-device-owner Other than that, I have no idea :\ – Idan Mar 01 '17 at 11:08
  • That uses shell commands... Thanks for help so far though :-) – Liran Friedman Mar 01 '17 at 11:32
0

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.

spico
  • 41
  • 7