Android UserManager: Check if user is owner (admin)

后端 未结 3 1322
刺人心
刺人心 2020-12-31 12:55

Im developing an app with the latest android version (4.2.1 API-Level 17) for tablets with multiuser capabilities.

I want to restrict certain features (like the acce

3条回答
  •  青春惊慌失措
    2020-12-31 13:17

    After researching further i found out that the multiuser api is not functional yet, it cant really be used for anything. there is a hack though for checking if the user is the owner using reflections:

    public boolean isCurrentUserOwner(Context context)
    {
        try
        {
            Method getUserHandle = UserManager.class.getMethod("getUserHandle");
            int userHandle = (Integer) getUserHandle.invoke(context.getSystemService(Context.USER_SERVICE));
            return userHandle == 0;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
    

    This works for me on the Nexus 7 and Nexus 10 with Android 4.2.1 Its very dirty. so i wouldnt recommend using it unless you are making an app thats device and version specific

提交回复
热议问题