Android 如何导入特定联系人';谁的电话号码?

Android 如何导入特定联系人';谁的电话号码?,android,uri,android-contacts,contactpicker,Android,Uri,Android Contacts,Contactpicker,我正在尝试读取使用联系人选择器选择的联系人的电话号码。 显示名称可以正常工作,但电话号码不行。 代码: 任何帮助都将非常感谢,因为我在任何地方都找不到相关的答案。如果您想让用户选择电话号码,最好的选择是使用电话选择器而不是联系人选择器: Intent intent = new Intent(Intent.ACTION_PICK, CommonDataKinds.Phone.CONTENT_URI); startActivityForResult(intent, PICK_PHONE); ...

我正在尝试读取使用联系人选择器选择的联系人的电话号码。 显示名称可以正常工作,但电话号码不行。 代码:


任何帮助都将非常感谢,因为我在任何地方都找不到相关的答案。

如果您想让用户选择电话号码,最好的选择是使用
电话选择器
而不是
联系人选择器

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

...

protected void onActivityResult(int requestCode, int resultCode, Intent intent){
    if (requestCode == PICK_PHONE && resultCode == RESULT_OK){
        Uri phoneUri = intent.getData();
        Cursor cur = getContentResolver().query(phoneUri, new String[] { Phone.DISPLAY_NAME, Phone.NUMBER }, null, null, null);
        if (cur != null && cur.moveToFirst()){
            String name = cur.getString(0);
            String number = cur.getString(1);
            Log.d("PHONE-PICKER", "User picker: " + name + " - " + number);
            cur.close();
        }
    }
}

如果要允许用户选择电话号码,最好的选择是使用
电话选择器
而不是
联系人选择器

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

...

protected void onActivityResult(int requestCode, int resultCode, Intent intent){
    if (requestCode == PICK_PHONE && resultCode == RESULT_OK){
        Uri phoneUri = intent.getData();
        Cursor cur = getContentResolver().query(phoneUri, new String[] { Phone.DISPLAY_NAME, Phone.NUMBER }, null, null, null);
        if (cur != null && cur.moveToFirst()){
            String name = cur.getString(0);
            String number = cur.getString(1);
            Log.d("PHONE-PICKER", "User picker: " + name + " - " + number);
            cur.close();
        }
    }
}
尝试以下方法:

private void retrieveContactNumber() {

  String contactNumber = null;

  // getting contacts ID
  Cursor cursorID = getContentResolver().query(uriContact,
        new String[]{ContactsContract.Contacts._ID},
        null, null, null);

  if (cursorID.moveToFirst()) {

    contactId = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts._ID));
  }

  cursorID.close();

  Log.d(TAG, "Contact ID: " + contactId);

  // Using the contact ID now we will get contact phone number
  Cursor cursorPhone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
        new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},

        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
                ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
                ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,

        new String[]{contactId}, null);

  if (cursorPhone.moveToFirst()) {
    contactNumber = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
  }

  cursorPhone.close();

  Log.d(TAG, "Contact Phone Number: " + contactNumber);
}
您应该在日志中看到联系人号码。

尝试以下方法:

private void retrieveContactNumber() {

  String contactNumber = null;

  // getting contacts ID
  Cursor cursorID = getContentResolver().query(uriContact,
        new String[]{ContactsContract.Contacts._ID},
        null, null, null);

  if (cursorID.moveToFirst()) {

    contactId = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts._ID));
  }

  cursorID.close();

  Log.d(TAG, "Contact ID: " + contactId);

  // Using the contact ID now we will get contact phone number
  Cursor cursorPhone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
        new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},

        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
                ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
                ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,

        new String[]{contactId}, null);

  if (cursorPhone.moveToFirst()) {
    contactNumber = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
  }

  cursorPhone.close();

  Log.d(TAG, "Contact Phone Number: " + contactNumber);
}

您应该在日志中看到联系人号码。

没有比“PICK_Phone”更有用的了。你救了我的另一天:)没有什么比“PICK_Phone”更像魅力了。你保存了我的另一天:)不返回我选择的联系人的电话号码,它总是选择ID==1的联系人。与电话号码相同。不返回我选择的联系人的电话号码,它总是选择ID==1的联系人。电话号码也一样。