how to get firstname and lastname of Android phone owner?

丶灬走出姿态 提交于 2019-12-30 01:16:23

问题


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.


回答1:


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




回答2:


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;
}



回答3:


I Had search on this thing and get some likes hope it will helpful to you.

  • How can I get the first name (or full name) of the user of the phone?

  • Android - Get UserData using AccountManager / First and Last name of the phone owner



来源:https://stackoverflow.com/questions/13084836/how-to-get-firstname-and-lastname-of-android-phone-owner

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!