【问题标题】:Get phone number from ContactsContract fails从 ContactsContract 获取电话号码失败
【发布时间】:2018-09-16 14:08:28
【问题描述】:

我正在对联系人使用 ACTION_PICK 意图,我想从选择的联系人中提取姓名和电话。

private static final int CONTANTS_REQUEST_CODE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_urgent_contacts);

    Intent contact_picker = new Intent(Intent.ACTION_PICK);
    contact_picker.setType(ContactsContract.Contacts.CONTENT_TYPE);
    startActivityForResult(contact_picker, CONTANTS_REQUEST_CODE);


}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == CONTANTS_REQUEST_CODE && resultCode == RESULT_OK){
        onContactPicked(data);
    }
}

private void onContactPicked(Intent data) {
    Cursor cursor = null;
    try {
        String contact_phoneNo =null;
        String contact_name;
        Uri uri = data.getData();
        cursor = getContentResolver().query(uri, null, null, null, null);
        if(cursor != null && cursor.moveToFirst()){
            int  nameIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
            contact_name = cursor.getString(nameIndex);

            Integer hasPhone = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
            String phone = null;
            if (hasPhone > 0) {
                int  phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                contact_phoneNo = cursor.getString(phoneIndex);


            }

            Toast.makeText(this, contact_name+" was added to your urgent contact list", Toast.LENGTH_SHORT).show();
            Toast.makeText(this, "phone number: "+contact_phoneNo, Toast.LENGTH_SHORT).show();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

重新接收联系人姓名可以正常工作。问题出在电话号码上。 该应用程序没有崩溃,但我在 Logcat 中看到指向 contact_phoneNo = cursor.getString(phoneIndex) 的错误

感谢您的帮助

【问题讨论】:

  • 可能重复:stackoverflow.com/questions/11218845/… - 您查询Contacts 表,但ContactsContract.CommonDataKinds.Phone.NUMBER 列在Data 表中。
  • 当我将其更改为 ContactsContract.CommonDataKinds.Phone.NUMBER 时,无论我选择什么,它都会显示相同的姓名和电话

标签: java android android-contacts


【解决方案1】:

选择的联系人可能没有电话,也可能有多个电话,在这种情况下,应用可能会获得与用户打算选择的电话不同的电话。

您应该使用 phone-picker 而不是 contact-picker,它允许用户选择联系人的特定电话,如下所示:

static final int REQUEST_SELECT_PHONE_NUMBER = 1;

public void selectContactPhone() {
    // Start an activity for the user to pick a phone number from contacts
    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType(CommonDataKinds.Phone.CONTENT_TYPE); // switches to PHONE PICKER
    startActivityForResult(intent, REQUEST_SELECT_PHONE_NUMBER);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_SELECT_PHONE_NUMBER && resultCode == RESULT_OK) {
        // Get the URI and query the content provider for the phone number
        Uri contactUri = data.getData();
        String[] projection = new String[]{CommonDataKinds.Phone.NUMBER};
        Cursor cursor = getContentResolver().query(contactUri, projection,
                null, null, null);
        // If the cursor returned is valid, get the phone number
        if (cursor != null && cursor.moveToFirst()) {
            int numberIndex = cursor.getColumnIndex(CommonDataKinds.Phone.NUMBER);
            String number = cursor.getString(numberIndex);
            // Do something with the phone number
            ...
        }
    }
}

更多信息请见:https://developer.android.com/guide/components/intents-common.html#Contacts

【讨论】:

    【解决方案2】:

    你可以为你的要求做的是,

    private void getContactsList() {
            Intent intent = new Intent(Intent.ACTION_PICK);
            intent.setType(String.valueOf(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE));
            startActivityForResult(intent, CONTANTS_REQUEST_CODE);
        }
    

    现在,获取所选联系人的详细信息,

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CONTANTS_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                Uri uri = data.getData();
                String[] projection = {ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME};
    
                Cursor cursor = getActivity().getContentResolver().query(uri, projection, null, null, null);
                cursor.moveToFirst();
    
                int nameColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
                name = cursor.getString(nameColumnIndex);
    
                int numberColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                number = cursor.getString(numberColumnIndex);
    
                if (TextUtils.isEmpty(number)) {
                    Cursor phones = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
                    while (phones.moveToNext()) {
                        number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    }
                    phones.close();
                }              
    
                Log.d("dataContact", " Name : " + name + ", Number : " + number);                                    
            }
        }
    }
    

    这是适合您情况的最佳解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-09
      • 2011-06-11
      • 2012-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-18
      • 1970-01-01
      • 2012-01-25
      相关资源
      最近更新 更多