1


我正在尝试使用此方法插入事件,它正在正确执行但联系人中没有联系人事件。

    private static void addEvent(ArrayList<ContentProviderOperation> contact_list,
        ContactDataBean contactBean, int rawContactID){
    contact_list.add(ContentProviderOperation
            .newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(
                    ContactsContract.Data.RAW_CONTACT_ID,
                    rawContactID)
            .withValue(ContactsContract.Data.MIMETYPE,
                    ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.Event.TYPE,
                    ContactsContract.CommonDataKinds.Event.TYPE_OTHER)
                    .withValue(ContactsContract.CommonDataKinds.Event.START_DATE,
                            contactBean.value).build());
try {
            // Executing all the insert operations as a single
            // database
            // transaction
            context.getContentResolver().applyBatch(
                    ContactsContract.AUTHORITY, contact_list);
            Toast.makeText(context, "Contact is successfully added",
                    Toast.LENGTH_SHORT).show();
        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (OperationApplicationException e) {
            e.printStackTrace();
        }
}


没有错误,也没有插入任何事件。
有什么问题??

4

2 回答 2

0

试试这个源代码,它可能会解决你的问题

https://android.googlesource.com/platform/packages/apps/Contacts/+/39948dc7e34dc2041b801058dada28fedb80c388/src/com/android/contacts/dialpad/DialpadFragment.java

于 2013-09-10T12:28:06.770 回答
0

您应该添加帐户(使用日历)或使用意图

使用意图:

Intent intent = new Intent(Intent.ACTION_EDIT);
        intent.setType("vnd.android.cursor.item/event");
        intent.putExtra("title", "Event with " + cname);
        intent.putExtra("beginTime", System.currentTimeMillis());
        intent.putExtra("endTime", System.currentTimeMillis() + 1800 * 1000);
        intent.putExtra("allDay", 0);
        intent.putExtra("hasAlarm", 1);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivityForResult(intent, 103);

或使用压延机

TimeZone tz = TimeZone.getDefault();
ContentResolver cr = getContentResolver();
                ContentValues values = new ContentValues();
                values.put(CalendarContract.Events.DTSTART, startMillis);
                values.put(CalendarContract.Events.DTEND, endMillis);
                values.put(CalendarContract.Events.TITLE, "Reminder - " + cname);
                values.put(CalendarContract.Events.DESCRIPTION, note);
                values.put(CalendarContract.Events.CALENDAR_ID, calID);
                values.put(CalendarContract.Events.EVENT_TIMEZONE, tz.getDisplayName());
                Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);

                long eventID = Long.parseLong(uri.getLastPathSegment());

                values = new ContentValues();
                values.put(CalendarContract.Reminders.MINUTES, 5);
                values.put(CalendarContract.Reminders.EVENT_ID, eventID);
                values.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
                uri = cr.insert(CalendarContract.Reminders.CONTENT_URI, values);
于 2013-09-10T12:32:10.230 回答