android 获取联系人和短消息信息

 
Cursor cursor = getContentResolver().query( 
                ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 
        while (cursor.moveToNext()) { 
 
            // 获得通讯录中每个联系人的ID 
            String contactId = cursor.getString(cursor 
                    .getColumnIndex(ContactsContract.Contacts._ID)); 
 
            // 获得通讯录中联系人的名字 
            String name = cursor 
                    .getString(cursor 
                            .getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 
            sb.append("名字:").append(name).append("\r\n"); 
 
            // 查看给联系人是否有电话,返回结果是String类型,1表示有,0表是没有 
            String hasPhone = cursor 
                    .getString(cursor 
                            .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
 
            if (hasPhone.equalsIgnoreCase("1")) 
                hasPhone = "true"; 
            else 
                hasPhone = "false"; 
 
            // 如果有电话,根据联系人的ID查找到联系人的电话,电话可以是多个 
 
            if (Boolean.parseBoolean(hasPhone)) { 
                Cursor phones = getContentResolver().query( 
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                        null, 
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID 
                                + " = " + contactId, null, null); 
                sb.append("电话:"); 
                while (phones.moveToNext()) { 
                    String phoneNumber = phones 
                            .getString(phones 
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
                    sb.append(phoneNumber+" "); 
                } 
                sb.append("\r\n"); 
                phones.close(); 
            } 
 
            // 查找email地址,这里email也可以有多个 
 
            Cursor emails = getContentResolver().query( 
                    ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
                    null, 
                    ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " 
                            + contactId, null, null); 
            sb.append("email:"); 
            while (emails.moveToNext()) { 
                String emailAddress = emails 
                        .getString(emails 
                                .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 
                sb.append(emailAddress+" "); 
            } 
            sb.append("\r\n"); 
            emails.close(); 
 
            // 获得联系人的地址 
 
            Cursor address = getContentResolver() 
                    .query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI, 
                            null, 
                            ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID 
                                    + " = " + contactId, null, null); 
            while (address.moveToNext()) { 
                // These are all private class variables, don’t forget to create 
                // them. 
                String poBox = address 
                        .getString(address 
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX)); 
                String street = address 
                        .getString(address 
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET)); 
                String city = address 
                        .getString(address 
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY)); 
                String state = address 
                        .getString(address 
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION)); 
                String postalCode = address 
                        .getString(address 
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE)); 
                String country = address 
                        .getString(address 
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY)); 
                String type = address 
                        .getString(address 
                                .getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE)); 
            }
 
        } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值