通过联系人选择器向 Android 中的 ContactsContract.CommonDataKinds.Event 添加事件

发布于 2024-12-26 13:20:05 字数 1654 浏览 3 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

征﹌骨岁月お 2025-01-02 13:20:05

以下是具体操作方法,假设您是这样调用联系人选择器的:

int CONTACT_PICKER_ID = 1001; //some arbitrary number that you choose
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivity(intent, CONTACT_PICKER_ID);

data.getData() 调用返回一个查找键,您必须查询联系人数据库才能查找联系人的 ID。

if (resultCode == RESULT_OK) {  
    switch (requestCode) {  
        case CONTACT_PICKER_ID:  
            Cursor cursor =  managedQuery(data.getData(), null, null, null, null);
            cursor.moveToFirst();
            //get the contact's ID
            contactId = Cursor.getLong(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

...

contactId 包含您想要的 ID。

Here's how to do it, assuming this is how you called the contact picker:

int CONTACT_PICKER_ID = 1001; //some arbitrary number that you choose
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivity(intent, CONTACT_PICKER_ID);

The data.getData() call returns a lookup key that you have to query the contacts DB for to find the contact's ID.

if (resultCode == RESULT_OK) {  
    switch (requestCode) {  
        case CONTACT_PICKER_ID:  
            Cursor cursor =  managedQuery(data.getData(), null, null, null, null);
            cursor.moveToFirst();
            //get the contact's ID
            contactId = Cursor.getLong(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

...

The contactId contains the ID you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文