获取联系人姓名?

时间:2016-01-30 04:04:45

标签: java android xml database android-intent

我想获取联系人姓名,但我无法。在查看this answer之后,我尝试使用系列,给定和显示来获取名称,但没有任何效果

  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == PICK_CONTACT && resultCode == RESULT_OK) {
            Uri contactUri = data.getData();
            Cursor cursor = getContentResolver().query(contactUri, null, null, null, null);
            cursor.moveToFirst(); //Move to first row...I actually dont know why this part is necessary, but I get an error without it... 
            int NumberColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); //Int column is the column of the numbers
            int NameColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
            String contactNumber = cursor.getString(NumberColumn);
            String contactName = cursor.getString(NameColumn);
            Toast.makeText(MainActivity.this, ""+ contactNumber +"" +contactName, Toast.LENGTH_SHORT).show();

        }

/

public void addContact(View v){ //OnClick listener to launch contact picker
    Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
    startActivityForResult(intent, PICK_CONTACT);
}

1 个答案:

答案 0 :(得分:0)

尝试使用以下代码获取特定号码的联系

...
.then (result) ->
  return result if result is someSpecialValue
  return Q()
    .then -> 
      ...
    .then -> 
      ...

更多详情请参阅以下链接https://tausiq.wordpress.com/2012/08/23/android-get-contact-details-id-name-phone-photo/