我做了什麼直到現在是:如何讓用戶從聯繫人中選擇一個電話號碼,然後得到的電話號碼,給定的名字和姓氏
點擊一些按鈕時:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, CONTACT_PICKER_RESULT);
則:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case CONTACT_PICKER_RESULT:
if (data != null) {
Uri uri = data.getData();
if (uri != null) {
Cursor cursor = null;
Cursor cursorStructuredName = null;
try{
cursor = getContentResolver().query(uri, null, null, null, null);
boolean hasNext = cursor.moveToNext();
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String hasPhoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
String phoneNumber = null;
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
if (phones.moveToFirst()) {//does not get inside the if
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
// projection
String[] projection = new String[] {ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME,
ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
ContactsContract.CommonDataKinds.StructuredName.PREFIX,
ContactsContract.CommonDataKinds.StructuredName.SUFFIX};
String where = ContactsContract.Data.RAW_CONTACT_ID + " =?";
String[] whereParameters = new String[]{contactId};
//Request
cursorStructuredName = getContentResolver().query(ContactsContract.Data.CONTENT_URI, projection, where, whereParameters, null);
String displayName = null;
String givenName = null;
String middleName = null;
String familyName = null;
String prefix = null;
String suffix = null;
hasNext = cursorStructuredName.moveToFirst();
if (cursorStructuredName != null && hasNext) {//does not get here because hasNext equals false
displayName = cursorStructuredName.getString(cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));
givenName = cursorStructuredName.getString(cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
middleName = cursorStructuredName.getString(cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME));
familyName = cursorStructuredName.getString(cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
prefix = cursorStructuredName.getString(cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.PREFIX));
suffix = cursorStructuredName.getString(cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.SUFFIX));
}
} finally {
if (cursor != null) {
cursor.close();
}
if (cursorStructuredName != null) {
cursorStructuredName.close();
}
}
}
}
break;
}
}
}
我不能得到的姓,給定的名稱等 我無法選擇的電話號碼。
正如我在標題中所說的,我想要的是,當用戶點擊某個按鈕時,聯繫人列表將被打開,當他選擇特定的聯繫人時,會出現此聯繫人電話號碼列表,然後當他選擇一個數字的方法onActivityResult將被調用,並有 我將「解析」電話號碼,姓氏,名稱等。
我建議你用較小的方法分割你的代碼。做一切的一個大方法是不好的做法。 –
你會得到一個例外嗎?發佈logcat。需要更多的信息 –
有沒有例外,問題是,我正在做一些worng(也許我沒有正確地查詢...)。 phones.moveToFirst()返回false,cursorStructuredName.moveToFirst()返回false。 –