当前位置: 代码迷 >> Android >> Android经验5.3-从通讯录获取和添加联系人
  详细解决方案

Android经验5.3-从通讯录获取和添加联系人

热度:76   发布时间:2016-05-01 14:55:49.0
Android心得5.3--从通讯录获取和添加联系人

1. 加入读取联系人信息的权限

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

content://com.android.contacts/contacts操作的数据是联系人信息Uri

content://com.android.contacts/data/phones联系人电话Uri

content://com.android.contacts/data/emails联系人EmailUri

读取联系人信息

2. 部分代码:

Cursorcursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, 

    null, null, null, null); 

  while (cursor.moveToNext()) { 

   String contactId =cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 

   String name =cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 

  

   Cursor phones =getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 

        null, 

        ContactsContract.CommonDataKinds.Phone.CONTACT_ID+" = "+ contactId, 

        null, null); 

    while (phones.moveToNext()) { 

     String phoneNumber =phones.getString(phones.getColumnIndex( 

        ContactsContract.CommonDataKinds.Phone.NUMBER)); 

     Log.i("RongActivity", "phoneNumber="+phoneNumber);

    } 

    phones.close(); 

    Cursor emails =getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, 

       null, 

       ContactsContract.CommonDataKinds.Email.CONTACT_ID+ " = " + contactId, 

       null,null); 

       while (emails.moveToNext()) { 

        // This would allow you get severalemail addresses 

        String emailAddress =emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

        Log.i("RongActivity","emailAddress="+ emailAddress);

       } 

       emails.close(); 

  } 

  cursor.close();

 

 

  相关解决方案