如何从联系人列表中获取特定的联系号码和姓名

时间:2013-04-08 17:14:39

标签: android android-intent

我能够检索联系人,但发生了点击特定联系人错误 这是我的代码:

contactButton.setOnClickListener(new View.OnClickListener() {
  public void onClick(View v) {
    Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
    startActivityForResult(intent, requestCode);
  }
});

和onActivity结果:

protected void onActivityResult(int request_Code, int resultCode, Intent data) {
  // TODO Auto-generated method stub
  super.onActivityResult(requestCode, resultCode, data);
  if (requestCode == request_Code) {
    if (resultCode == RESULT_OK) {
      Uri contactData = data.getData();
      Cursor c = managedQuery(contactData, null, null, null, null);
      if (c.moveToFirst()) {
        String id = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
        String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
        if (hasPhone.equalsIgnoreCase("1")) {
          Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id, null, null);
          phones.moveToFirst();
          String phn_no = phones.getString(phones.getColumnIndex("data1"));
          String name = c.getString(c.getColumnIndex(StructuredPostal.DISPLAY_NAME));
          Toast.makeText(this, "contact info : " + phn_no + "\n" + name, Toast.LENGTH_LONG).show();
        }
      }
    }
  }
}

0 个答案:

没有答案