我正在尝试使用此方法插入事件,它正在正确执行,但联系人中没有联系事件。
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();
}
}
没有错误,没有插入任何事件。
有什么问题?
答案 0 :(得分:0)
试试这个源代码,它可能会清除你的问题
答案 1 :(得分: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);
或使用calander
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);