obtaining the user name of any user

190 Views Asked by At

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.

1

There are 1 best solutions below

6
On

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]