How to get the first name and last name from Android contacts?

时间:2021-01-31 22:55:03

How to get the following fields from Android contacts? I used Android 2.2.

如何从Android联系人中获取以下字段?我使用的是Android 2.2。

  1. Name prefix
  2. First name
  3. Middle name
  4. Last name
  5. Name prefix
  6. Phonetic given name
  7. 语音给出了名字

  8. Phonetic middle name
  9. 拼音中间名

  10. Phonetic family name
  11. 语音姓氏

8 个解决方案

#1


54  

Look at ContactsContract.CommonDataKinds.StructuredName class. You can find there all columns you are looking for. Try sth like this:

查看ContactsContract.CommonDataKinds.StructuredName类。您可以在那里找到您要查找的所有列。试试这样:

    String whereName = ContactsContract.Data.MIMETYPE + " = ?";
    String[] whereNameParams = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE };
    Cursor nameCur = contentResolver.query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
    while (nameCur.moveToNext()) {
        String given = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
        String family = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
        String display = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));
    }
    nameCur.close();

It returns all names in contacts. To be more precise you can add a contact id as an additional parameter to the query - you will get address for particular contact.

它返回联系人中的所有名称。更准确地说,您可以将联系人ID添加为查询的附加参数 - 您将获得特定联系人的地址。

#2


16  

For a specified contact you can do this :

对于指定的联系人,您可以这样做:

String whereName = ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID + " = ?";
String[] whereNameParams = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE, contact_ID };
Cursor nameCur = contentResolver.query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
while (nameCur.moveToNext()) {
    String given = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
    String family = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
    String display = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));
}
nameCur.close();

#3


6  

try to use this code to get the required information about the contact,the code is here-

尝试使用此代码获取有关联系人的所需信息,代码在此处 -

import android.provider.ContactsContract.Contacts;
import android.database.Cursor;

// Form an array specifying which columns to return, you can add more.
String[] projection = new String[] {
                         ContactsContract.Contacts.DISPLAY_NAME,
                         ContactsContract.CommonDataKinds.Phone
                         ContactsContract.CommonDataKinds.Email
                      };

Uri contacts =  ContactsContract.Contacts.CONTENT_LOOKUP_URI;
// id of the Contact to return.
long id = 3;

// Make the query. 
Cursor managedCursor = managedQuery(contacts,
                     projection, // Which columns to return 
                     null,       // Which rows to return (all rows)
                                 // Selection arguments (with a given ID)
                     ContactsContract.Contacts._ID = "id", 
                                 // Put the results in ascending order by name
                     ContactsContract.Contacts.DISPLAY_NAME + " ASC");

#4


6  

As the other example (just for fun) but for fetching contact name of a single user:

作为另一个例子(只是为了好玩)但是为了获取单个用户的联系人姓名:

// A contact ID is fetched from ContactList
Uri resultUri = data.getData(); 
Cursor cont = getContentResolver().query(resultUri, null, null, null, null);
if (!cont.moveToNext()) {   
    Toast.makeText(this, "Cursor contains no data", Toast.LENGTH_LONG).show(); 
                return;
}
int columnIndexForId = cont.getColumnIndex(ContactsContract.Contacts._ID);
String contactId = cont.getString(columnIndexForId);

// Fetch contact name with a specific ID
String whereName = ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID + " = " + contactId; 
String[] whereNameParams = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE };
Cursor nameCur = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
while (nameCur.moveToNext()) {
    String given = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
    String family = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
    String display = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));
    Toast.makeText(this, "Name: " + given + " Family: " +  family + " Displayname: "  + display, Toast.LENGTH_LONG).show();
}
nameCur.close();
cont.close();

#5


3  

Some links to get you started, in addition to the suggestions from Raunak:

除了Raunak的建议之外,还有一些链接可以帮助您入门:

#6


1  

Experimenting with the ContactsContract.Data.CONTENT_URI in late 2015 on marshmallow. I'm unable to get the GIVEN_NAME or similar fields. I think the later apis have deprecated these. Run the following code to print out the columns you have on your phone

在2015年底在marshmallow上试用ContactsContract.Data.CONTENT_URI。我无法获取GIVEN_NAME或类似字段。我认为后来的apis已经弃用了这些。运行以下代码以打印出手机上的列

Uri uri = ContactsContract.Data.CONTENT_URI;
String selection = ContactsContract.Data.MIMETYPE + " = ?";
String[] selectionArgs = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE};
Cursor cursor = contentResolver.query(
            uri,       // URI representing the table/resource to be queried
            null,      // projection - the list of columns to return.  Null means "all"
            selection, // selection - Which rows to return (condition rows must match)
            selectionArgs,      // selection args - can be provided separately and subbed into selection.
            null);   // string specifying sort order

if (cursor.getCount() == 0) {
  return;
}
Log.i("Count:", Integer.toString(cursor.getCount())); // returns number of names on phone

while (cursor.moveToNext()) {
  // Behold, the firehose!
  Log.d(TAG, "-------------------new record\n");
  for(String column : cursor.getColumnNames()) {
    Log.d(TAG, column + ": " + cursor.getString(cursor.getColumnIndex(column)) + "\n");
  }
}

#7


0  

Check here there is example code exactly for that: http://developer.android.com/guide/topics/ui/layout/listview.html

请在此处查看完整的示例代码:http://developer.android.com/guide/topics/ui/layout/listview.html

#8


0  

try this,

public void onActivityResult(int reqCode, int resultCode, Intent data) { super.onActivityResult(reqCode, resultCode, data);

    try {
        if (resultCode == Activity.RESULT_OK) {
            Uri contactData = data.getData();
            Cursor cur = managedQuery(contactData, null, null, null, null);
            ContentResolver contect_resolver = getContentResolver();

            if (cur.moveToFirst()) {
                String id = cur.getString(cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
                String name = "";
                String no = "";

                Cursor phoneCur = contect_resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);

                if (phoneCur.moveToFirst()) {
                    name = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    no = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                }

                Log.e("Phone no & name :***: ", name + " : " + no);
                txt.append(name + " : " + no + "\n");

                id = null;
                name = null;
                no = null;
                phoneCur = null;
            }
            contect_resolver = null;
            cur = null;
            //                      populateContacts();
        }
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
        Log.e("IllegalArgumentException::", e.toString());
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("Error :: ", e.toString());
    }
}

#1


54  

Look at ContactsContract.CommonDataKinds.StructuredName class. You can find there all columns you are looking for. Try sth like this:

查看ContactsContract.CommonDataKinds.StructuredName类。您可以在那里找到您要查找的所有列。试试这样:

    String whereName = ContactsContract.Data.MIMETYPE + " = ?";
    String[] whereNameParams = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE };
    Cursor nameCur = contentResolver.query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
    while (nameCur.moveToNext()) {
        String given = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
        String family = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
        String display = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));
    }
    nameCur.close();

It returns all names in contacts. To be more precise you can add a contact id as an additional parameter to the query - you will get address for particular contact.

它返回联系人中的所有名称。更准确地说,您可以将联系人ID添加为查询的附加参数 - 您将获得特定联系人的地址。

#2


16  

For a specified contact you can do this :

对于指定的联系人,您可以这样做:

String whereName = ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID + " = ?";
String[] whereNameParams = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE, contact_ID };
Cursor nameCur = contentResolver.query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
while (nameCur.moveToNext()) {
    String given = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
    String family = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
    String display = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));
}
nameCur.close();

#3


6  

try to use this code to get the required information about the contact,the code is here-

尝试使用此代码获取有关联系人的所需信息,代码在此处 -

import android.provider.ContactsContract.Contacts;
import android.database.Cursor;

// Form an array specifying which columns to return, you can add more.
String[] projection = new String[] {
                         ContactsContract.Contacts.DISPLAY_NAME,
                         ContactsContract.CommonDataKinds.Phone
                         ContactsContract.CommonDataKinds.Email
                      };

Uri contacts =  ContactsContract.Contacts.CONTENT_LOOKUP_URI;
// id of the Contact to return.
long id = 3;

// Make the query. 
Cursor managedCursor = managedQuery(contacts,
                     projection, // Which columns to return 
                     null,       // Which rows to return (all rows)
                                 // Selection arguments (with a given ID)
                     ContactsContract.Contacts._ID = "id", 
                                 // Put the results in ascending order by name
                     ContactsContract.Contacts.DISPLAY_NAME + " ASC");

#4


6  

As the other example (just for fun) but for fetching contact name of a single user:

作为另一个例子(只是为了好玩)但是为了获取单个用户的联系人姓名:

// A contact ID is fetched from ContactList
Uri resultUri = data.getData(); 
Cursor cont = getContentResolver().query(resultUri, null, null, null, null);
if (!cont.moveToNext()) {   
    Toast.makeText(this, "Cursor contains no data", Toast.LENGTH_LONG).show(); 
                return;
}
int columnIndexForId = cont.getColumnIndex(ContactsContract.Contacts._ID);
String contactId = cont.getString(columnIndexForId);

// Fetch contact name with a specific ID
String whereName = ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID + " = " + contactId; 
String[] whereNameParams = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE };
Cursor nameCur = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
while (nameCur.moveToNext()) {
    String given = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
    String family = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
    String display = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));
    Toast.makeText(this, "Name: " + given + " Family: " +  family + " Displayname: "  + display, Toast.LENGTH_LONG).show();
}
nameCur.close();
cont.close();

#5


3  

Some links to get you started, in addition to the suggestions from Raunak:

除了Raunak的建议之外,还有一些链接可以帮助您入门:

#6


1  

Experimenting with the ContactsContract.Data.CONTENT_URI in late 2015 on marshmallow. I'm unable to get the GIVEN_NAME or similar fields. I think the later apis have deprecated these. Run the following code to print out the columns you have on your phone

在2015年底在marshmallow上试用ContactsContract.Data.CONTENT_URI。我无法获取GIVEN_NAME或类似字段。我认为后来的apis已经弃用了这些。运行以下代码以打印出手机上的列

Uri uri = ContactsContract.Data.CONTENT_URI;
String selection = ContactsContract.Data.MIMETYPE + " = ?";
String[] selectionArgs = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE};
Cursor cursor = contentResolver.query(
            uri,       // URI representing the table/resource to be queried
            null,      // projection - the list of columns to return.  Null means "all"
            selection, // selection - Which rows to return (condition rows must match)
            selectionArgs,      // selection args - can be provided separately and subbed into selection.
            null);   // string specifying sort order

if (cursor.getCount() == 0) {
  return;
}
Log.i("Count:", Integer.toString(cursor.getCount())); // returns number of names on phone

while (cursor.moveToNext()) {
  // Behold, the firehose!
  Log.d(TAG, "-------------------new record\n");
  for(String column : cursor.getColumnNames()) {
    Log.d(TAG, column + ": " + cursor.getString(cursor.getColumnIndex(column)) + "\n");
  }
}

#7


0  

Check here there is example code exactly for that: http://developer.android.com/guide/topics/ui/layout/listview.html

请在此处查看完整的示例代码:http://developer.android.com/guide/topics/ui/layout/listview.html

#8


0  

try this,

public void onActivityResult(int reqCode, int resultCode, Intent data) { super.onActivityResult(reqCode, resultCode, data);

    try {
        if (resultCode == Activity.RESULT_OK) {
            Uri contactData = data.getData();
            Cursor cur = managedQuery(contactData, null, null, null, null);
            ContentResolver contect_resolver = getContentResolver();

            if (cur.moveToFirst()) {
                String id = cur.getString(cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
                String name = "";
                String no = "";

                Cursor phoneCur = contect_resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);

                if (phoneCur.moveToFirst()) {
                    name = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    no = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                }

                Log.e("Phone no & name :***: ", name + " : " + no);
                txt.append(name + " : " + no + "\n");

                id = null;
                name = null;
                no = null;
                phoneCur = null;
            }
            contect_resolver = null;
            cur = null;
            //                      populateContacts();
        }
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
        Log.e("IllegalArgumentException::", e.toString());
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("Error :: ", e.toString());
    }
}