添加事件和提醒功能无法在LG Nexus 5 Marshmallow中使用此例外,除了Nexus此代码适用于所有设备
我叫这个方法
addEvent(this, Calendar.getInstance().getTimeInMillis()+600000, Calendar.getInstance().getTimeInMillis()+900000, titleTimeArr[0]);
异常来自于setReminder方法 当控制来到这条线上时
Uri uri = cr.insert(CalendarContract.Reminders.CONTENT_URI, values);
代码
public void addEvent(Context context,long startcalDate,long endcalDate,String title) {
try {
ContentResolver cr = context.getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, startcalDate);
values.put(CalendarContract.Events.DTEND, endcalDate);
values.put(CalendarContract.Events.TITLE,title);
values.put(CalendarContract.Events.CALENDAR_ID, 1);
values.put(CalendarContract.Events.EVENT_TIMEZONE, Calendar.getInstance().getTimeZone().getID());
System.out.println(Calendar.getInstance().getTimeZone().getID());
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
// Save the eventId into the Task object for possible future delete.
Long eventId = Long.parseLong(uri.getLastPathSegment());
// Add a 5 minute, 1 hour and 1 day reminders (3 reminders)
setReminder(cr, eventId, 5);
setReminder(cr, eventId, 60);
// setReminder(cr, eventId, 1440);
} catch (Exception e) {
e.printStackTrace();
}
}
public void setReminder(ContentResolver cr, long eventID, int timeBefore) {
try {
ContentValues values = new ContentValues();
values.put(CalendarContract.Reminders.MINUTES, timeBefore);
values.put(CalendarContract.Reminders.EVENT_ID, eventID);
values.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
Uri uri = cr.insert(CalendarContract.Reminders.CONTENT_URI, values); // Exception comes in this line
Cursor c = CalendarContract.Reminders.query(cr, eventID,
new String[]{CalendarContract.Reminders.MINUTES});
if (c.moveToFirst()) {
System.out.println("calendar"
+ c.getInt(c.getColumnIndex(CalendarContract.Reminders.MINUTES)));
}
c.close();
} catch (Exception e) {
e.printStackTrace();
}
}
例外
08-18 15:12:57.429 8740-8740/com.sntialtech.hondagbs W/System.err: android.database.sqlite.SQLiteException
08-18 15:12:57.435 8740-8740/com.sntialtech.hondagbs W/System.err: at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:179)
08-18 15:12:57.435 8740-8740/com.sntialtech.hondagbs W/System.err: at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
08-18 15:12:57.436 8740-8740/com.sntialtech.hondagbs W/System.err: at android.content.ContentProviderProxy.insert(ContentProviderNative.java:476)
08-18 15:12:57.436 8740-8740/com.sntialtech.hondagbs W/System.err: at android.content.ContentResolver.insert(ContentResolver.java:1231)
08-18 15:12:57.436 8740-8740/com.sntialtech.hondagbs W/System.err: at com.sntialtech.hondagbs.activities.InclusionScheduleActivity.setReminder(InclusionScheduleActivity.java:307)
08-18 15:12:57.436 8740-8740/com.sntialtech.hondagbs W/System.err: at com.sntialtech.hondagbs.activities.InclusionScheduleActivity.addEvent(InclusionScheduleActivity.java:292)
08-18 15:12:57.436 8740-8740/com.sntialtech.hondagbs W/System.err: at com.sntialtech.hondagbs.activities.InclusionScheduleActivity.parseTimeAndAddEvent(InclusionScheduleActivity.java:211)
08-18 15:12:57.436 8740-8740/com.sntialtech.hondagbs W/System.err: at com.sntialtech.hondagbs.activities.InclusionScheduleActivity.onPressPermissionOk(InclusionScheduleActivity.java:140)
08-18 15:12:57.436 8740-8740/com.sntialtech.hondagbs W/System.err: at com.sntialtech.hondagbs.utils.Utils.showPermissionDialog(Utils.java:98)
08-18 15:12:57.436 8740-8740/com.sntialtech.hondagbs W/System.err: at com.sntialtech.hondagbs.activities.InclusionScheduleActivity.onItemClick(InclusionScheduleActivity.java:178)
08-18 15:12:57.436 8740-8740/com.sntialtech.hondagbs W/System.err: at com.sntialtech.hondagbs.adapters.InclusionScheduleAdapter$1.onClick(InclusionScheduleAdapter.java:88)
08-18 15:12:57.436 8740-8740/com.sntialtech.hondagbs W/System.err: at android.view.View.performClick(View.java:5204)
08-18 15:12:57.436 8740-8740/com.sntialtech.hondagbs W/System.err: at android.view.View$PerformClick.run(View.java:21153)
08-18 15:12:57.436 8740-8740/com.sntialtech.hondagbs W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
08-18 15:12:57.436 8740-8740/com.sntialtech.hondagbs W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
08-18 15:12:57.436 8740-8740/com.sntialtech.hondagbs W/System.err: at android.os.Looper.loop(Looper.java:148)
08-18 15:12:57.436 8740-8740/com.sntialtech.hondagbs W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
08-18 15:12:57.436 8740-8740/com.sntialtech.hondagbs W/System.err: at java.lang.reflect.Method.invoke(Native Method)
08-18 15:12:57.436 8740-8740/com.sntialtech.hondagbs W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
08-18 15:12:57.436 8740-8740/com.sntialtech.hondagbs W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:1)
这个答案帮助我解决了同样的问题:Cant set a calendar event on android 4.0 >
分别使用CalendarContract.EXTRA_EVENT_BEGIN_TIME,CalendarContract.EXTRA_EVENT_END_TIME替换Events.DTSTART,Events.DTEND。