Is there a way to get the firstname and lastname of Android phone owner? I've search the internet but I have no luck. I stumbled upon this question in Stackoverlow but this is getting firstname and lastname of all contacts. What I need is just to get the owner's firstname and lastname.
Asked
Active
Viewed 1.1k times
3 Answers
23
I think this is available ICS onwards only - Look at this for more info http://android-codelabs.appspot.com/resources/tutorials/contactsprovider/ex1.html
Cursor c = activity.getContentResolver().query(ContactsContract.Profile.CONTENT_URI, null, null, null, null);
int count = c.getCount();
String[] columnNames = c.getColumnNames();
boolean b = c.moveToFirst();
int position = c.getPosition();
if (count == 1 && position == 0) {
for (int j = 0; j < columnNames.length; j++) {
String columnName = columnNames[j];
String columnValue = c.getString(c.getColumnIndex(columnName)));
...
//Use the values
}
}
c.close();
Android includes a personal profile that represents the device owner - this profile is known as the "Me" profile and is stored in the ContactsContract.Profile
table. You can read data from the user's profile so long as you
add the READ_PROFILE and READ_CONTACTS permission in your AndroidManifest.xml.
The most relevant fields for you are the DISPLAY_NAME column from the Contact and possibly the StructuredName fields

Ushal Naidoo
- 2,704
- 1
- 24
- 37

Vrashabh Irde
- 14,129
- 6
- 51
- 103
-
at least, how can i execute this code if it is ICS? ContactsContract.Profile.CONTENT_URI is giving me error. – silent_coder14 Oct 30 '12 at 23:19
-
You have to add android:targetSdkVersion="16" to your uses-sdk in the manifest since it is ICS and above – Vrashabh Irde Oct 31 '12 at 05:23
-
Did it work for you ? Or do you need more help setting it up ? – Vrashabh Irde Oct 31 '12 at 05:34
3
Try this:
final AccountManager manager = AccountManager.get(this);
final Account[] accounts = manager.getAccountsByType("com.google");
final int size = accounts.length;
String[] names = new String[size];
for (int i = 0; i < size; i++) {
names[i] = accounts[i].name;
}

daniel
- 1,435
- 9
- 16
3
I Had search on this thing and get some likes hope it will helpful to you.

Community
- 1
- 1

Chintan Khetiya
- 15,962
- 9
- 47
- 85