문제

I'm working on Android 4.2.2.

Say I have a contact called "Home". I use a basic contact picker such as:

Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));

...and then in the onActivityResult I get the contact URI:

String contactUri = data.getDataString();

Let's say it comes back with content://com.android.contacts/data/10855.

But now in another part of the application, I'm watching for incoming calls. When the call comes in I'm trying to find the calling contact URI:

Cursor c = context.getContentResolver().query(
             Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone_nbr)),
             new String[] {PhoneLookup.LOOKUP_KEY,PhoneLookup._ID},
             null, null, null);

I was guessing that the 10855 would be in either LOOKUP_KEY or _ID. It happens to be neither, so I guess I'm going about it the wrong way. Can someone advise?

Thanks!

도움이 되었습니까?

해결책

Looking at a similar answer I'd say to use the URI returned and find the lookup key from there.

Something like this might work:

Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null, null, null);
String key = null;
if (c != null && c.moveToFirst()) { 
  key = c.getString(c.getColumnIndex(ContactsContract.Data.LOOKUP_KEY));
} 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top