android–startActivityForResult的一个联系人的所有电话号码

android–startActivityForResult的一个联系人的所有电话号码,第1张

概述联系人可能有许多电话号码(移动电话,家庭电话……).我想让用户选择一个特定联系人的电话号码.通过此片段,我可以获得每个联系人的所有电话号码列表.Intentintent=newIntent(Intent.ACTION_PICK,ContactsContract.CommonDataKinds.Phone.CONTENT_URI);startActivityFo

联系人可能有许多电话号码(移动电话,家庭电话……).我想让用户选择一个特定联系人的电话号码.

通过此片段,我可以获得每个联系人的所有电话号码列表.

Intent intent = new Intent(Intent.ACTION_PICK,     ContactsContract.CommonDataKinds.Phone.CONTENT_URI);startActivityForResult(intent, PHONE_NUMBER_PICKED);

如何仅列出一个联系人的电话号码?

编辑:我知道如何获取联系人的所有电话号码,这不是重点.我可以将所有电话号码放在列表视图中,让用户选择一个.但是这个功能存在(如上所述),我只是不想要所有号码,而只需要一个联系人的电话号码.

解决方法:

如果您想获得与联系人相关的所有电话号码,请:

1)使用此意图打开联系人应用程序:

 Intent intent = new Intent(Intent.ACTION_PICK);    intent.setType(ContactsContract.Contacts.CONTENT_TYPE);    startActivityForResult(intent, PICK_CONTACT);

2)在onActivityResult中使用以下代码:

if (requestCode == PICK_CONTACT) {            if (resultCode == Activity.RESulT_OK) {                if (data != null) {                    Uri contactData = data.getData();                    try {                        String ID = contactData.getLastPathSegment();                        Cursor phoneCur = getContentResolver()                                .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,                                        null,                                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID                                                + " = ?", new String[] { ID },                                        null);                        final ArrayList<String> phonesList = new ArrayList<String>();                        while (phoneCur.movetoNext()) {                            // This would allow you get several phone addresses                            // if the phone addresses were stored in an array                            String phone = phoneCur                                    .getString(phoneCur                                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));                            phonesList.add(phone);                        }                        phoneCur.close();                        if (phonesList.size() == 0) {                            Helper.showToast(                                    this,                                    getString(R.string.error_no_phone_no_in_contact),                                    Toast.LENGTH_LONG);                        } else if (phonesList.size() == 1) {                            editText.setText(phonesList.get(0));                        } else {                            final String[] phonesArr = new String[phonesList                                    .size()];                            for (int i = 0; i < phonesList.size(); i++) {                                phonesArr[i] = phonesList.get(i);                            }                            AlertDialog.Builder dialog = new AlertDialog.Builder(                                    SendSMS.this);                            dialog.setTitle(R.string.choose_phone);                            ((Builder) dialog).setItems(phonesArr,                                    new DialogInterface.OnClickListener() {                                        public voID onClick(                                                DialogInterface dialog,                                                int which) {                                            String selectedEmail = phonesArr[which];                                            editText.setText(selectedEmail);                                        }                                    }).create();                            dialog.show();                        }                    } catch (Exception e) {                        Log.e("fileS", "Failed to get phone data", e);                    }                }            }        }

这将在名为editText的编辑文本中设置所选手机号码.您可以根据需要更改此设置.

总结

以上是内存溢出为你收集整理的android – startActivityForResult的一个联系人的所有电话号码全部内容,希望文章能够帮你解决android – startActivityForResult的一个联系人的所有电话号码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1099804.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存