zoukankan      html  css  js  c++  java
  • 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();

        }

       
    }

  • 相关阅读:
    天地图OGC WMTS服务规则
    【异常】VS中运行HTTP 无法注册URL
    【TextBox】重写右键菜单
    【ContextMenu】DataContext不自动更新
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
  • 原文地址:https://www.cnblogs.com/honeynm/p/3784183.html
Copyright © 2011-2022 走看看