【问题标题】:Opening contacts and aquiring phone number打开联系人并获取电话号码
【发布时间】:2011-11-09 00:54:24
【问题描述】:

我正在尝试在我的应用程序中打开我的联系人并允许用户选择一个联系人,然后用户将能够选择他们想要使用的电话号码。目前我的方法允许我选择正确的联系人和电话号码,但是我不知道如何告诉用户该电话号码是家庭电话号码还是手机号码。

如果是小区,家庭,工作等,我如何在数字旁边提及

这就是我现在用来从联系人那里获取电话号码的方法。谢谢!

protected void onActivityResult(final int requestCode, int resultCode,
        Intent data) {
    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) {
                    Toast.makeText(this, "No Phone Number in Contact",
                            Toast.LENGTH_SHORT).show();
                } else if (phonesList.size() == 1) {
                    switch (requestCode) {
                    case (1):
                        edit1.setText(phonesList.get(0));
                        break;
                    case (2):
                        edit2.setText(phonesList.get(0));
                        break;
                    case (3):
                        edit3.setText(phonesList.get(0));
                        break;
                    }
                } 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(
                            Settings.this);
                    dialog.setTitle("Choose Phone Number");
                    ((Builder) dialog).setItems(phonesArr,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    String selectedEmail = phonesArr[which];
                                    switch (requestCode) {
                                    case (1):
                                        edit1.setText(selectedEmail);
                                        break;
                                    case (2):
                                        edit2.setText(selectedEmail);
                                        break;
                                    case (3):
                                        edit3.setText(selectedEmail);
                                        break;
                                    }
                                }
                            }).create();
                    dialog.show();
                }
            } catch (Exception e) {
                Log.e("FILES", "Failed to get phone data", e);
            }
        }
    }

【问题讨论】:

    标签: android contacts


    【解决方案1】:
    ContentResolver contentResolver = getContentResolver();
    
        Cursor cursor = contentResolver.query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER,
                ContactsContract.CommonDataKinds.Phone.TYPE },
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "="
                + contactId, null, null + " DESC");
    
        if (cursor.moveToFirst()) {
            do {
            int phoneNumberColumn = cursor.getColumnIndex(Phone.NUMBER);
            int phoneTypecolumn = cursor.getColumnIndex(Phone.TYPE);
    
            int phoneType = Integer.parseInt(cursor
                .getString(phoneTypecolumn));
    
            String phoneNumberType = "";
    
            if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_HOME) {
                phoneNumberType = "Home";
            } else if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) {
                phoneNumberType = "Mobile";
            } .....//Similarly the other types could be obtained.
    
            } while (cursor.moveToNext());
        }
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2015-01-24
      • 1970-01-01
      • 2014-03-30
      • 2016-12-26
      • 2013-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多