自行编写的获取系统通讯录中联系人,并且打开拨号盘的功能

首先,按钮添加点击事件,事件为调用系统通讯录,代码如下:

private void startContact() {
        //调用系统通讯录
        Intent intent = new Intent();

        intent.setAction(Intent.ACTION_PICK);

        intent.setData(ContactsContract.Contacts.CONTENT_URI);

        startActivityForResult(intent, REQUEST_CONTACT);

    }

之后处理返回后的数据事件,代码如下:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == REQUEST_CONTACT) {

            if (resultCode == RESULT_OK) {

                if (data == null) {

                    return;

                }
                 ContentResolver reContentResolverol = getContentResolver();
                
                Uri contactData = data.getData();
                
                Cursor cursor = managedQuery(contactData, null, null, null, null);  
                
                cursor.moveToFirst();
                userName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                Cursor phone = reContentResolverol.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = " + contactId, null, null);
                List<String> list = new ArrayList<String>();
                while (phone.moveToNext()) {
                    userNumber = phone
                            .getString(phone
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    list.add(userNumber);
                    tvName.setText(list.toString() + " (" + userName + ")");
                }
                
//                Uri result = data.getData();
//                contactId = result.getLastPathSegment();
//                result.getUserInfo();
//                contactName = result.getUserInfo();
//                tvName.setText(contactName);
//
//                //进入系统拨号盘界面
//                Intent intent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:" + userNumber));
//                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                startActivity(intent);

                  //Toast弹窗显示获取的联系人的所有号码和联系人姓名

                  showToast(list.toString() + " (" + userName + ")");
            }

        }
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值