android 读取联系人

设置读取权限

<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 读取联系人,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。