0

可能重复:
如何在 Android 2.0 上读取联系人

我只想要List<String>Android 设备联系人列表中的名字 + 姓氏。应该很简单,但我找不到任何基本代码。

只是在寻找:

Steve Smith
Joe Shmo
Blah Davis

等等都来自用户手机上的联系人。我已经添加了:

<uses-permission android:name="android.permission.READ_CONTACTS" /> 
4

1 回答 1

1
    public List<String> getFullContactName(){

    List<String> name = new List<String>();

    String[] projection = new String[] {Data.DATA2, Data.DATA3};
    String where = Data.MIMETYPE + "='" + StructuredName.CONTENT_ITEM_TYPE + "'";
    Uri uri = Data.CONTENT_URI;

    ContentResolver contentResolver = context.getContentResolver();
    // Make the query. 
    Cursor cursor = contentResolver.query(uri,
                             projection, // Which columns to return 
                             where,       // Which rows to return
                             null,       // Selection arguments (none)
                             // Put the results in ascending order by name
                             null);

    String firstName, LastName;
    while (cursor.moveToNext()) {

        firstName = cursor.getString(cursor.getColumnIndex(Data.DATA2));
        lastName = cursor.getString(cursor.getColumnIndex(Data.DATA3));
name.add(firstName + " " + lastName);           
    }

    cursor.close();
    cursor = null;
    return name;
}
于 2012-10-22T20:39:50.603 回答