通过联系人选择器向 Android 中的 ContactsContract.CommonDataKinds.Event 添加事件
在我的 Android 应用程序的活动中,我想打开内容选择器。当用户选择这些联系人之一时,应该有一个新条目(类型为“other”的事件)被插入到表ContactsContract.CommonDataKinds.Event中。
启动联系人选择器意图很容易。但随后必须获取所选联系人的一些数据并在事件表中创建一个新条目。这是我到目前为止的代码,不幸的是它不起作用:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case CONTACT_PICKER_ID:
Uri contactData = data.getData();
//String contactID = contactData.getLastPathSegment();
// ADD A NEW BIRTHDAY FOR THE SELECTED CONTACT START
ContentValues values = new ContentValues();
values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE);
values.put(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_OTHER);
values.put(ContactsContract.CommonDataKinds.Event.CONTACT_ID, 250);
values.put(ContactsContract.CommonDataKinds.Event.START_DATE, "2012-12-12");
Uri dataUri = getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
// ADD A NEW BIRTHDAY FOR THE SELECTED CONTACT END
break;
}
}
}
此代码需要“WRITE_CONTACTS”权限。
联系人 ID“250”是硬编码的。当然,应该从与联系人选择器结果一起发送的意图数据中检索它。
上面的代码以 NullPointerException 终止。为什么会这样呢?如何从意图中获取联系人的 ID,以便我可以使用它来插入新行?
编辑:此外,该行 ...
getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
... 引发异常。那里出了什么问题?
In an activity of my Android application, I would like to open the content picker. And when the user selects one of those contacts, there should be a new entry (an event with type "other") that is inserted into the table ContactsContract.CommonDataKinds.Event.
Starting the contact picker intent is easy. But then one must get some data for the selected contact and create a new entry in the event table. This is the code I have so far, unfortunately it doesn't work:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case CONTACT_PICKER_ID:
Uri contactData = data.getData();
//String contactID = contactData.getLastPathSegment();
// ADD A NEW BIRTHDAY FOR THE SELECTED CONTACT START
ContentValues values = new ContentValues();
values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE);
values.put(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_OTHER);
values.put(ContactsContract.CommonDataKinds.Event.CONTACT_ID, 250);
values.put(ContactsContract.CommonDataKinds.Event.START_DATE, "2012-12-12");
Uri dataUri = getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
// ADD A NEW BIRTHDAY FOR THE SELECTED CONTACT END
break;
}
}
}
This code requires the permission "WRITE_CONTACTS".
The contact ID "250" is hard-coded. Of course, it should be retrieved from the intent data that is sent along with the contact picker's result.
The code above terminates with a NullPointerException. Why is this so? And how do I get the contact's id from the intent so that I can use it for inserting the new row?
Edit: Additionally, the line ...
getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
... throws an exception. What's wrong there?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是具体操作方法,假设您是这样调用联系人选择器的:
data.getData() 调用返回一个查找键,您必须查询联系人数据库才能查找联系人的 ID。
contactId 包含您想要的 ID。
Here's how to do it, assuming this is how you called the contact picker:
The data.getData() call returns a lookup key that you have to query the contacts DB for to find the contact's ID.
The contactId contains the ID you want.