2012-10-22 118 views
0

可能重複:
How to read contacts on Android 2.0如何獲取Android上聯繫人的姓名列表?

我只是想在Android上的設備的聯繫人列表中的名字+姓氏的List<String>。應該是非常簡單的東西,但我找不到任何基本代碼。

只是在尋找:

Steve Smith 
Joe Shmo 
Blah Davis 

等一切從用戶的手機上的聯繫人。我已經添加:

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

見http://stackoverflow.com/questions/2356084/read-all-contacts-phone-numbers-in-android – justderb

回答

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

謝謝,但這似乎成爲拉動除了電話應用程序中的更多聯繫人外,還有很多聯繫人進入了列表。任何方式來獲取我的手機應用程序中出現的那些? –

+0

如果通過電話應用程序,您的意思是本地聯繫人應用程序,那麼這段代碼會帶來聯繫。 – Hardik4560

相關問題