2

I am currently using the following code to obtain the current user name:

        String username;
        {
            Cursor c = ctx.getContentResolver().query(Profile.CONTENT_URI,
                new String[]{Profile.DISPLAY_NAME}, null, null, null);
            if (c.getCount() > 0)
            {
                c.moveToFirst();
                username = c.getString(0);
            }

On my test device (Samsung Galaxy Tab Pro), with the correct permissions, this works well for the "owner" user, as well as other non-restricted users. However, things seem to fall apart for limited user profiles:

  • The code always returns zero cursor rows. I had hoped that enabling access to the Contacts app would allow it to work, but that has no effect.
  • At any rate, unlike non-limited profiles, the profile of the limited user account does not seem to be connected to the name displayed on the lock screen, so even if I could get this code to work I'm not sure how useful it would be.

Please forgive the multi-part question, but:

  • Is this a peculiarity of the Samsung tablet, or is this behavior consistent across devices?
  • Is there any way to read the profile of a limited user?
  • Is there a better way to obtain the current user name?

Any insight appreciated.

j__m
  • 8,784
  • 1
  • 26
  • 49

1 Answers1

1

I have tried to search about it & can say it's not possible to get details of "Restricted Profile (User)".

In background when you try to read details for "Restricted Profile user" they are trying to change in database and throw SQLiteexception : delete then re-write existing database throws SQLiteReadOnlyDatabaseException on getWritableDatabase. due to this exception I don't think there is chance to read user name.

Without use your above code you can get directly call getUsername() of UserManager class.[API Level 17]

Chintan Khetiya
  • 15,208
  • 9
  • 41
  • 81
  • Unfortunately, for reasons unknown and probably unknowable, `getUserName()` requires `MANAGE_USERS` permission, which is impossible to get for SDK apps. – j__m Apr 30 '15 at 06:22
  • Yes, if we have chunk of code then why we should go for more api level. Let me know if you get any luck on it. – Chintan Khetiya Apr 30 '15 at 06:32
  • OK, this is strange. I said earlier that "the profile of the limited user account does not seem to be connected to the name displayed on the lock screen", and that was true then. However, my lock screen has now started displaying the name from the profile, and my code is now working to get the user name! I guess there's some strange lag between creating a profile for a limited user and having that profile work? I'm definitely leaving this question open to see if anyone has anything to add, particularly about other device models. – j__m Apr 30 '15 at 06:37
  • it's relay strange. good that now it's working. have you change to add anything? then pls share. – Chintan Khetiya Apr 30 '15 at 07:09
  • I changed the name in the limited user's profile again. My code kept reading the old name for a long time. Finally it is seeing the update. There's something really weird about how this all works (or doesn't work) within a limited account. – j__m Apr 30 '15 at 07:32
  • My code hasn't changed. It's just been me playing with the tablet. – j__m Apr 30 '15 at 07:32