내장 된 활동에서 ID 번호를 검색 한 후 연락처의 전화 번호를 가져 오려고합니다. 그러나 내가 아래 코드에서 커서를 사용하여 데이터베이스를 쿼리 할 때마다 - 내가 선택한 연락처의 모바일 번호가 있더라도 반환 된 행은 0이됩니다.연락처 전화 번호 검색 Android의 URI에서
누군가 더 나은 방향으로 나를 가리키거나 연락처의 전화 번호를 얻는 방법에 대한 예를 보여줄 수 있습니까?
내 코드 :
private Runnable getSMSRunnable() {
return new Runnable() {
public void run() {
Intent i = new Intent(Intent.ACTION_PICK,
ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(i, CONTACTS_REQUEST_CODE);
}
};
}
로그 출력 i가 함께 ID로부터 전화 번호를 반환하도록 설계된 방법에 ID (6802)를 통과한다에서
content://com.android.contacts/data/6802
를 리턴 반환
public static String getContactPhoneNumberByPhoneType(Context context, long contactId, int type) {
String phoneNumber = null;
String[] whereArgs = new String[] { String.valueOf(contactId), String.valueOf(type) };
Log.d(TAG, String.valueOf(contactId));
Cursor cursor = context.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? and "
+ ContactsContract.CommonDataKinds.Phone.TYPE + " = ?", whereArgs, null);
int phoneNumberIndex = cursor
.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER);
Log.d(TAG, String.valueOf(cursor.getCount()));
if (cursor != null) {
Log.v(TAG, "Cursor Not null");
try {
if (cursor.moveToNext()) {
Log.v(TAG, "Moved to first");
Log.v(TAG, "Cursor Moved to first and checking");
phoneNumber = cursor.getString(phoneNumberIndex);
}
} finally {
Log.v(TAG, "In finally");
cursor.close();
}
}
Log.v(TAG, "Returning phone number");
return phoneNumber;
}
(이 경우 ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE에) 타입 제공 전화 번호는 null입니다. 즉, 액세스하려는 행을 찾을 수 없다는 뜻입니다. 즉, 내 검색어에 문제가 있음을 의미합니다. 그러나 휴대 전화 번호가있는 연락처를 확인하면 어떻게됩니까? 0 행 쿼리를 얻으시겠습니까?
도움을 주시면 감사하겠습니다. 정말 고맙습니다!
설명이 명확하지 않은 경우에도 작동합니다. 왜 전화.CONTACT_ID (contact_id)가 CONTACTS._ID (_id)와 일치하지 않습니다 – redochka
이렇게 설명하면 더 쉽습니다. '_ID'는 데이터베이스 용이고 'CONTACT_ID'는 해당 테이블을 쿼리하지 않을 때 연락처 ID 용입니다. . 이게 도움이 되나요? – hwrdprkns
결론 : Phones._ID == Contacts._ID – redochka