码迷,mamicode.com
首页 > 移动开发 > 详细

android 读取联系人

时间:2014-06-16 00:50:21      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:android   ext   com   get   string   os   

设置读取权限

<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 读取联系人

标签:android   ext   com   get   string   os   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!