Android获取手机联系人的基本信息(如姓名、电话、邮箱、备注)

在做项目的过程中,需要获取我们手机通讯录联系人的基本信息,如姓名、电话、邮箱、备注、昵称、公司、职位、家庭电话等等信息,下面就是我总结的一些具体方法。

1:首先读取联系人需要添加读取权限,6.0以上需要动态获取权限)

1.1AndroidManifest.xml 文件添加:




1.2 在我们需要获取联系人信息功能的地方添加:

//判断用户是否已经授权给我们了 如果没有,调用下面方法向用户申请授权,之后系统就会弹出一个权限申请的对话框
        if (ContextCompat.checkSelfPermission(this,Manifest.permission.READ_CONTACTS)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(
                    MainActivity.this,new String[]{Manifest.permission.READ_CONTACTS},1);
        } else {
                  //在这里执行读取通讯录方法
        }

1.3:在class中添加权限回调方法
//回调方法,无论哪种结果,最终都会回调该方法,之后在判断用户是否授权,
    @Override
    public void onRequestPermissionsResult(int requestCode,String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 1:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                  //在这里执行读取通讯录方法
                } else {
                    Toast.makeText(this, "获取联系人权限失败", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
        }
    }

2:这里我做了一个工具类,获取到联系人的所有具体信息,具体如下

import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.CommonDataKinds.Im;
import android.provider.ContactsContract.CommonDataKinds.Nickname;
import android.provider.ContactsContract.CommonDataKinds.Note;
import android.provider.ContactsContract.CommonDataKinds.Organization;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

public class ContactType {
    private final Context context;
    private JSONObject contactData;
    private JSONObject jsonObject;

    public ContactType(Context context) {
        this.context = context;
    }

    //获取联系人所有信息(这里返回String,你也可以直接返回其他类型改改就可以了)
    public String getInformation() throws JSONException {
        contactData = new JSONObject();
        int num = 0;
        // 获得所有的联系人
        Cursor cur = context.getContentResolver().query(
                ContactsContract.Contacts.CONTENT_URI,
                null,
                null,
                null,
                ContactsContract.Contacts.DISPLAY_NAME
                        + " COLLATE LOCALIZED ASC");
        // 循环遍历
        if (cur.moveToFirst()) {
            int idColumn = cur.getColumnIndex(ContactsContract.Contacts._ID);
            int displayNameColumn = cur
                    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
            do {
                jsonObject = new JSONObject();
                contactData.put("information" + num, jsonObject);
                num++;
                // 获得联系人的ID号
                String contactId = cur.getString(idColumn);
                // 获得联系人姓名
                String disPlayName = cur.getString(displayNameColumn);

                // 查看该联系人有多少个电话号码。如果没有这返回值为0
                int phoneCount = cur
                        .getInt(cur
                                .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                jsonObject.put("name", disPlayName);
                if (phoneCount > 0) {
                    // 获得联系人的电话号码
                    Cursor phones = context.getContentResolver().query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                    + " = " + contactId, null, null);
                    if (phones.moveToFirst()) {
                        do {
                            // 遍历所有的电话号码
                            int phoneType = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)); // 手机
                            // 住宅电话
                            if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_HOME) {
                                String homeNum = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                jsonObject.put("homeNum", homeNum);
                            }
                            // 单位电话
                            if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_WORK) {
                                String jobNum = phones.getString(phones
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                jsonObject.put("jobNum", jobNum);
                            }
                            // 单位传真
                            if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK) {
                                String workFax = phones.getString(phones
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                jsonObject.put("workFax", workFax);
                            }
                            // 住宅传真
                            if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME) {
                                String homeFax = phones.getString(phones
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                                jsonObject.put("homeFax", homeFax);
                            } // 寻呼机
                            if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_PAGER) {
                                String pager = phones.getString(phones
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                jsonObject.put("pager", pager);
                            }
                            // 回拨号码
                            if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_CALLBACK) {
                                String quickNum = phones.getString(phones
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                jsonObject.put("quickNum", quickNum);
                            }
                            // 公司总机
                            if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_COMPANY_MAIN) {
                                String jobTel = phones.getString(phones
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                jsonObject.put("jobTel", jobTel);
                            }
                            // 车载电话
                            if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_CAR) {
                                String carNum = phones.getString(phones
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                jsonObject.put("carNum", carNum);
                            } // ISDN
                            if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_ISDN) {
                                String isdn = phones.getString(phones
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                jsonObject.put("isdn", isdn);
                            } // 总机
                            if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_MAIN) {
                                String tel = phones.getString(phones
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                jsonObject.put("tel", tel);
                            }
                            // 无线装置
                            if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_RADIO) {
                                String wirelessDev = phones.getString(phones
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                                jsonObject.put("wirelessDev", wirelessDev);
                            } // 电报
                            if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_TELEX) {
                                String telegram = phones.getString(phones
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                jsonObject.put("telegram", telegram);
                            }
                            // TTY_TDD
                            if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_TTY_TDD) {
                                String tty_tdd = phones.getString(phones
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                jsonObject.put("tty_tdd", tty_tdd);
                            }
                            // 单位手机
                            if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE) {
                                String jobMobile = phones.getString(phones
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                jsonObject.put("jobMobile", jobMobile);
                            }
                            // 单位寻呼机
                            if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_WORK_PAGER) {
                                String jobPager = phones.getString(phones
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                jsonObject.put("jobPager", jobPager);
                            } // 助理
                            if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_ASSISTANT) {
                                String assistantNum = phones.getString(phones
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                jsonObject.put("assistantNum", assistantNum);
                            } // 彩信
                            if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_MMS) {
                                String mms = phones.getString(phones
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                jsonObject.put("mms", mms);
                            }

                            String mobileEmail = phones.getString(phones
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                            jsonObject.put("mobileEmail", mobileEmail);
                            String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                            jsonObject.put("phoneNumber", phoneNumber);

                        } while (phones.moveToNext());
                    }
                }
                // 获取该联系人邮箱
                Cursor emails = context.getContentResolver().query(
                        ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = " + contactId, null, null);
                if (emails.moveToFirst()) {
                    do {
                        // 遍历所有的电话号码
//                        String emailType = emails
//                                .getString(emails
//                                        .getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
                        String emailValue = emails
                                .getString(emails
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
//                        jsonObject.put("emailType", emailType);
                        jsonObject.put("emailValue", emailValue);

                    } while (emails.moveToNext());
                }

                // 获取该联系人IM
                Cursor IMs = context.getContentResolver().query(
                        Data.CONTENT_URI,
                        new String[]{Data._ID, Im.PROTOCOL, Im.DATA},
                        Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='"
                                + Im.CONTENT_ITEM_TYPE + "'",
                        new String[]{contactId}, null);
                if (IMs.moveToFirst()) {
                    do {
                        String protocol = IMs.getString(IMs
                                .getColumnIndex(Im.PROTOCOL));
                        String date = IMs
                                .getString(IMs.getColumnIndex(Im.DATA));
                        jsonObject.put("protocol", protocol);
                        jsonObject.put("date", date);

                    } while (IMs.moveToNext());
                }

                // 获取该联系人地址
                Cursor address = context.getContentResolver()
                        .query(
                                ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,
                                null,
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                        + " = " + contactId, null, null);
                if (address.moveToFirst()) {
                    do {
                        // 遍历所有的地址
                        String street = address
                                .getString(address
                                        .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
                        String city = address
                                .getString(address
                                        .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
                        String region = address
                                .getString(address
                                        .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
                        String postCode = address
                                .getString(address
                                        .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
                        String formatAddress = address
                                .getString(address
                                        .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
                        jsonObject.put("street", street);
                        jsonObject.put("city", city);
                        jsonObject.put("region", region);
                        jsonObject.put("postCode", postCode);
                        jsonObject.put("formatAddress", formatAddress);

                    } while (address.moveToNext());
                }
                // 获取该联系人组织
                Cursor organizations = context.getContentResolver().query(
                        Data.CONTENT_URI,
                        new String[]{Data._ID, Organization.COMPANY,
                                Organization.TITLE},
                        Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='"
                                + Organization.CONTENT_ITEM_TYPE + "'",
                        new String[]{contactId}, null);
                if (organizations.moveToFirst()) {
                    do {
                        String company = organizations.getString(organizations
                                .getColumnIndex(Organization.COMPANY));
                        String title = organizations.getString(organizations
                                .getColumnIndex(Organization.TITLE));
                        jsonObject.put("company", company);
                        jsonObject.put("title", title);

                    } while (organizations.moveToNext());
                }

                // 获取备注信息
                Cursor notes = context.getContentResolver().query(
                        Data.CONTENT_URI,
                        new String[]{Data._ID, Note.NOTE},
                        Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='"
                                + Note.CONTENT_ITEM_TYPE + "'",
                        new String[]{contactId}, null);
                if (notes.moveToFirst()) {
                    do {
                        String noteinfo = notes.getString(notes
                                .getColumnIndex(Note.NOTE));
                        jsonObject.put("noteinfo", noteinfo);

                    } while (notes.moveToNext());
                }

                // 获取nickname信息
                Cursor nicknames = context.getContentResolver().query(
                        Data.CONTENT_URI,
                        new String[]{Data._ID, Nickname.NAME},
                        Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='"
                                + Nickname.CONTENT_ITEM_TYPE + "'",
                        new String[]{contactId}, null);
                if (nicknames.moveToFirst()) {
                    do {
                        String nickname_ = nicknames.getString(nicknames
                                .getColumnIndex(Nickname.NAME));
                        jsonObject.put("nickname", nickname_);

                    } while (nicknames.moveToNext());
                }
            } while (cur.moveToNext());
        }
        Log.e("联系人信息=====", contactData.toString());
        return contactData.toString();
    }

}

3:用法很简单:

MoblieType mobiletype= new MoblieType(this);
//得到联系人字符串
String contactinfo=mobiletype.getContactInfo();

获取到的数据如下:

{
    "information0":{
    "name":"侯敏", "mobileEmail":"34346464664", "phoneNumber":"34346464664"
},"information1":{
    "name":"科大讯飞", "mobileEmail":"424554548484", "phoneNumber":"424554548484", "homeNum":
    "12234567890", "jobNum":"13334566", "homeFax":"158575757777", "workFax":
    "424554548484", "emailValue":"[email protected]", "protocol":"1", "date":"灵敏", "street":
    "你明明敏敏婆婆", "formatAddress":"你明明敏敏婆婆", "company":"大田", "title":"程序员", "noteinfo":
    "二货", "nickname":"啥子"
}
}

好了,告辞

你可能感兴趣的:(Android开发功能记录,Android,Android获取联系人信息)