Search code examples
androidusermanager

Android: Check if Current User is a guest


I had a read through UserManager and UserHandle docs and couldn't find any method which would return a boolean answering the question. Anyone have an idea how to determine this?


Solution

  • UserInfo class has “isGuest()” method returning a Boolean and UserManager.getUsers() returns a list, of UserInfo type. BUT, all these methods are hidden and not supported for 3rd party apps.

    Additionally, the getUsers() method requires “android.Manifest.permission.MANAGE_USERS” permission, which is meant for only system (or any apps that have the platform key, or if your app only runs on Android emulator, as it gets a default platform key). So, even if you find any way to sign your application with the platform signature, it would definitely break on one or more Android versions.

    Although You can try with reflection to get the access for getUsers() like below:

    UserManager manager = (UserManager)getSystemService(USER_SERVICE);
    Method method = manager.getClass().getMethod("getUsers", null);
    Object users = method.invoke(manager, null);
    

    But, again there seem no guarantees you will get what you want here.