【问题标题】:How to get phone number from contact in android如何从android中的联系人获取电话号码
【发布时间】:2017-08-28 09:03:34
【问题描述】:

我正在尝试在 android 中获取电话号码作为字符串,我成功联系并从他那里获取电话号码,但日志中的结果是 data1,号码是 32821。我不明白我的问题。

这是我的代码:

public void getContact(View view) {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, 10);
    }

    Intent contactsIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
    this.pickContact = 1;
    startActivityForResult(contactsIntent, this.pickContact);
}

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);

    if(reqCode == this.pickContact) {
        if (resultCode == Activity.RESULT_OK) {
            Log.d("ContactsH", "ResOK");
            Uri contactData = data.getData();
            Cursor contact =  getContentResolver().query(contactData, null, null, null, null);

            if (contact.moveToFirst()) {
                String phoneNumber = ContactsContract.CommonDataKinds.Phone.NUMBER;

                Log.d("ContactsH", "Calling to:"+phoneNumber);
                contact.close();
                this.callByNumber(phoneNumber);
            }
        }
    } else {
        Log.d("ContactsH", "Canceled");
    }
}

有什么帮助吗?

【问题讨论】:

  • String name = contact.getString(contact.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 没有给你名字吗?你没有为这个号码尝试过同样的事情吗?
  • 很抱歉,这对我不起作用,因为无论我选择谁,它都显示相同的数字。
  • 是的,我确实尝试过,但它给了我例外
  • @cricket_007 很抱歉,你能具体写信给我吗?

标签: java android android-contacts google-contacts-api


【解决方案1】:

感谢@Levon Petrosyan

但我只需要添加他的link 中的部分并将其复制到我的函数中。

这是工作代码:

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data){
    super.onActivityResult(reqCode, resultCode, data);

    if(reqCode == this.pickContact){
        if (resultCode == Activity.RESULT_OK) {
            Log.d("ContactsH", "ResOK");
            Uri contactData = data.getData();
            Cursor contact =  getContentResolver().query(contactData, null, null, null, null);

            if (contact.moveToFirst()) {
                String name = contact.getString(contact.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                // TODO Whatever you want to do with the selected contact's name.

                ContentResolver cr = getContentResolver();
                Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                        "DISPLAY_NAME = '" + name + "'", null, null);
                if (cursor.moveToFirst()) {
                    String contactId =
                            cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                    //
                    //  Get all phone numbers.
                    //
                    Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
                    while (phones.moveToNext()) {
                        String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        int type = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                        switch (type) {
                            case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
                                // do something with the Home number here...
                                break;
                            case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
                                // do something with the Mobile number here...
                                Log.d("ContactsH", number);
                                this.callByNumber(number);
                                break;
                            case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
                                // do something with the Work number here...
                                break;
                        }
                    }
                    phones.close();
                }
                cursor.close();
            }
        }
    }else{
        Log.d("ContactsH", "Canceled");
    }
}

【讨论】:

    猜你喜欢
    • 2014-03-30
    • 2015-04-01
    • 1970-01-01
    • 2014-05-26
    • 2017-04-13
    • 2015-07-20
    • 1970-01-01
    • 2012-06-28
    • 2016-02-16
    相关资源
    最近更新 更多