如何在Android中打开联系人卡片ID
问题描述:
是否可以通过联系人ID打开安卓联系人卡片?它适用于电话号码。下面是一个例子,如果我用如何在Android中打开联系人卡片ID
Intent i = new Intent();
i.setAction(ContactsContract.Intents.SHOW_OR_CREATE_CONTACT);
i.setData(Uri.fromParts("tel", "123456", null)); //<---- Change here from Phone to IDcontext.startActivity(i);
但我想通过ID打开这个名片,例如,如果从联系人的电话号码会改变。
答
使用ACTION_VIEW并使用联系人ID建立联系人URI或使用联系人查找URI(如果您已拥有该联系人)(首选)。
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactID));
intent.setData(uri);
context.startActivity(intent);
答
您可以使用下面的URI:
Uri.Builder newUriBuilder = ContactsContract.Contacts.CONTENT_LOOKUP_URI.buildUpon();
newUriBuilder.appendPath("/").appendPath(theContactKey)
i.setData(newUriBuilder.build());
你会发现这个URI是如何工作的通过看API documentation for CONTENT_LOOKUP_URI更多细节。
答
我试图用这里列出的方法打开联系人卡片,但不知何故联系人活动在打开后立即关闭。
它似乎是联系活动不接受我的旧内容uri。
我使用ContactsContract.Contacts
类的getLookupUri (long contactId, String lookupKey)
方法以获得正确的内容URI https://developer.android.com/reference/android/provider/ContactsContract.Contacts.html#getLookupUri(long, java.lang.String)
所以打开名片的代码变得解决了这个问题:
Intent intent = new Intent(Intent.ACTION_VIEW);
String lookupKey = phonesCursor.getString(phonesCursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup.LOOKUP_KEY));
long contactId = phonesCursor.getLong(phonesCursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID));
Uri uri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);
intent.setData(uri);
startActivity(intent);
好的,谢谢你jhominal为指向正确的方向,但你的答案并不完全正确。这里是正确的解决方案:Uri look = Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI,hereTheLookupKey); Intent i = new Intent(Intent.ACTION_VIEW); i.setData(的样子); – 2010-11-30 15:13:20
没有更好地使用这个URI:Contacts.getLookupUri(contactId,lookupKey)? – 2016-12-31 18:51:16