首页 > 移动平台 > 详细

android 读取联系人

时间:2014-06-16 00:48:32      阅读:448      评论:0      收藏:0      [点我收藏+]

设置读取权限

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

 

主要代码

public void readContacts() {

   

    Cursor cursor = this.getContentResolver().query(
            ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

    int contactIdIndex = 0;

    int nameIndex = 0;

    if (cursor.getCount() > 0) {

        contactIdIndex = cursor
                .getColumnIndex(ContactsContract.Contacts._ID);

        nameIndex = cursor
                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);

    }

    while (cursor.moveToNext()) {

        String contactId = cursor.getString(contactIdIndex);

        String name = cursor.getString(nameIndex);
       
        Toast.makeText(this, name, 1000).show();

        Cursor phones = this.getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?",
                new String[] { contactId }, null);
       
        if(phones.moveToNext()){
           
            int phoneIndex=phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
           
            String phoneNumber=phones.getString(phoneIndex);
           
            Toast.makeText(this, phoneNumber, 1000).show();
        }
       
        phones.close();
       
       
        Cursor email = this.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=?",
                new String[] { contactId }, null);
       
        if(email.moveToNext()){
           
            int emailIndex =email.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
           
            String emailAddress=email.getString(emailIndex);
           
            Toast.makeText(this, emailAddress, 1000).show();
        }
       
        email.close();

    }

   
}

android 读取联系人,布布扣,bubuko.com

android 读取联系人

原文:http://www.cnblogs.com/honeynm/p/3784183.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!