将联系人事件添加到 Android 的联系人表中

发布于 2024-12-29 00:08:14 字数 2736 浏览 1 评论 0原文

我努力编写了以下代码,但不幸的是,该条目被分配给了错误的联系人。我不知道为什么。测试了小时天,但找不到错误。你能帮助我吗?

我想使用代码从联系人列表中选择一个人(使用联系人选择器),然后将此人的事件条目(出生日期)添加到联系人表中。

第 1 步:

我已经在清单文件中设置了权限:

<uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>

第 2 步:

定义了联系人选择器的 ID:

private static final int CONTACT_PICKER_ID = 123;

第 3 步:

调用联系人选择器:

Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_ID);

第 4 步:

另一种方法侦听联系人选择器的结果并尝试为所选用户添加事件:

@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 selectedPerson = data.getData();
                String contactId = selectedPerson.getLastPathSegment();
                // ADD A NEW EVENT FOR THE SELECTED CONTACT --- BEGIN
                ContentValues values = new ContentValues();
                values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE);
                values.put(ContactsContract.Data.RAW_CONTACT_ID, contactId);
                values.put(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY);
                values.put(ContactsContract.CommonDataKinds.Event.RAW_CONTACT_ID, contactId);
                values.put(ContactsContract.CommonDataKinds.Event.LABEL, "");
                values.put(ContactsContract.CommonDataKinds.Event.START_DATE, "2010-01-28"); // hard-coded date of birth
                Uri created = null;
                try {
                    created = getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
                }
                catch (Exception e) {
                }
                if (created == null) {
                    Toast.makeText(this.getApplicationContext(), "Failed inserting the event!", Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(this.getApplicationContext(), "Successfully inserted the event!", Toast.LENGTH_SHORT).show();
                }
                // ADD A NEW EVENT FOR THE SELECTED CONTACT --- END
            break;
        }
    }
}

该事件已成功插入到数据库中,并且还显示在 Google 联系人中 -但不幸的是它被分配给了错误的 接触。为什么会这样呢?我从联系人选择器获取的 contactId 是否错误?

I've worked hard on the following code, but unfortunately, the entry is assigned to the wrong contact. I don't know why. Tested for hours days but can't find the mistake. Can you help me?

I would like to use the code in order to select a person from the contact list (using the contact picker) and then adding an event entry (date of birth) for this person to the contacts table.

Step 1:

I've already set the permission in the manifest file:

<uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>

Step 2:

The contact picker's ID is defined:

private static final int CONTACT_PICKER_ID = 123;

Step 3:

The contact picker is called:

Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_ID);

Step 4:

Another method listens for the contact picker's result and tries to add an event for the selected user:

@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 selectedPerson = data.getData();
                String contactId = selectedPerson.getLastPathSegment();
                // ADD A NEW EVENT FOR THE SELECTED CONTACT --- BEGIN
                ContentValues values = new ContentValues();
                values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE);
                values.put(ContactsContract.Data.RAW_CONTACT_ID, contactId);
                values.put(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY);
                values.put(ContactsContract.CommonDataKinds.Event.RAW_CONTACT_ID, contactId);
                values.put(ContactsContract.CommonDataKinds.Event.LABEL, "");
                values.put(ContactsContract.CommonDataKinds.Event.START_DATE, "2010-01-28"); // hard-coded date of birth
                Uri created = null;
                try {
                    created = getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
                }
                catch (Exception e) {
                }
                if (created == null) {
                    Toast.makeText(this.getApplicationContext(), "Failed inserting the event!", Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(this.getApplicationContext(), "Successfully inserted the event!", Toast.LENGTH_SHORT).show();
                }
                // ADD A NEW EVENT FOR THE SELECTED CONTACT --- END
            break;
        }
    }
}

The event is successfully inserted to the database and also shown in the Google contacts - but unfortunately it's assigned to the wrong contact. Why is this so? Is my contactId wrong that I get from the contact picker?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

メ斷腸人バ 2025-01-05 00:08:14

您从联系人选择器返回的活动结果是联系人的完整路径。类似于:

content://com.android.contacts/contacts/lookup/0r7-2C46324E483C324A3A484634/7

这就是您的内容:

Uri selectedPerson = data.getData();

其中包含联系人的 LOOKUP_KEY 和联系人的 _ID。但是,在插入数据表时,您需要使用 RawContacts _ID。您需要做的是获取 RawContact 的 _ID:

long rawContactId = -1;
Cursor c = getContentResolver().query(RawContacts.CONTENT_URI,
      new String[]{RawContacts._ID},
      RawContacts.CONTACT_ID + "=?",
      new String[]{String.valueOf(contactId)}, null);
try {
    if (c.moveToFirst()) {
        rawContactId = c.getLong(0);
    }
} finally {
    c.close();
}

然后使用 rawContactId

values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);

但是,应该注意的是,每个联系人可以有多个 RawContact。您可能需要调整代码,以便为每个 RawContact 添加一个事件。

The activity result that you get back from the contact picker is the full path to the contact. Something like:

content://com.android.contacts/contacts/lookup/0r7-2C46324E483C324A3A484634/7

This is what's in your:

Uri selectedPerson = data.getData();

This contains both the Contact's LOOKUP_KEY AND the Contact's _ID. However, you need to be using the RawContacts _ID when inserting into the Data table. What you need to do is grab the RawContact's _ID:

long rawContactId = -1;
Cursor c = getContentResolver().query(RawContacts.CONTENT_URI,
      new String[]{RawContacts._ID},
      RawContacts.CONTACT_ID + "=?",
      new String[]{String.valueOf(contactId)}, null);
try {
    if (c.moveToFirst()) {
        rawContactId = c.getLong(0);
    }
} finally {
    c.close();
}

And then use the rawContactId:

values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);

However, it should be noted that there can be multiple RawContacts per one Contact. You may want to adjust your code so that it adds an event for each RawContact.

染墨丶若流云 2025-01-05 00:08:14

您的内容值中有一项是错误的。您在 onActivityResult 数据变量中获取的 Uri 不是 raw_contact_id 而是联系人 ID。两者之间的区别在于一个联系人可以包含多个原始联系人。原始联系人与单个帐户(例如 Google 或 Facebook)相关联。但一个联系人可以包含多个 raw_contacts。

            Uri selectedPerson = data.getData();
            String contactId = selectedPerson.getLastPathSegment();
            // ADD A NEW EVENT FOR THE SELECTED CONTACT --- BEGIN
            ContentValues values = new ContentValues();
            values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE);
            ***values.put(ContactsContract.Data.CONTACT_ID, contactId);***
            values.put(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY);
            values.put(ContactsContract.CommonDataKinds.Event.RAW_CONTACT_ID, contactId);
            values.put(ContactsContract.CommonDataKinds.Event.LABEL, "");
            values.put(ContactsContract.CommonDataKinds.Event.START_DATE, "2010-01-28"); // hard-coded date of birth

One entry is wrong in your content values. The Uri which you get in your onActivityResult data variable is not the raw_contact_id but the contact id. The difference between both is that one contact can contain multiple raw contacts. A raw contact is associated with a single account like Google or Facebook. But a contact can contain multiple raw_contacts.

            Uri selectedPerson = data.getData();
            String contactId = selectedPerson.getLastPathSegment();
            // ADD A NEW EVENT FOR THE SELECTED CONTACT --- BEGIN
            ContentValues values = new ContentValues();
            values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE);
            ***values.put(ContactsContract.Data.CONTACT_ID, contactId);***
            values.put(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY);
            values.put(ContactsContract.CommonDataKinds.Event.RAW_CONTACT_ID, contactId);
            values.put(ContactsContract.CommonDataKinds.Event.LABEL, "");
            values.put(ContactsContract.CommonDataKinds.Event.START_DATE, "2010-01-28"); // hard-coded date of birth
老子叫无熙 2025-01-05 00:08:14

尝试切换...

Uri created = null;
try {
     created = getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
}catch (Exception e) {
}
            if (created == null) {
                Toast.makeText(this.getApplicationContext(), "Failed inserting the event!", Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(this.getApplicationContext(), "Successfully inserted the event!", Toast.LENGTH_SHORT).show();
            }

到...

try{
    Uri created = getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
    if (created == null) {
         Toast.makeText(this.getApplicationContext(), "Failed inserting the event!", Toast.LENGTH_SHORT).show();
    }else{
         Toast.makeText(this.getApplicationContext(), "Successfully inserted the event!", Toast.LENGTH_SHORT).show();
    }
}catch (Exception e) {}

try switching...

Uri created = null;
try {
     created = getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
}catch (Exception e) {
}
            if (created == null) {
                Toast.makeText(this.getApplicationContext(), "Failed inserting the event!", Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(this.getApplicationContext(), "Successfully inserted the event!", Toast.LENGTH_SHORT).show();
            }

to...

try{
    Uri created = getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
    if (created == null) {
         Toast.makeText(this.getApplicationContext(), "Failed inserting the event!", Toast.LENGTH_SHORT).show();
    }else{
         Toast.makeText(this.getApplicationContext(), "Successfully inserted the event!", Toast.LENGTH_SHORT).show();
    }
}catch (Exception e) {}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文