问题描述
我正在制作一个可访问android日历并创建事件的应用.
I am making an app that access the android calendar and creates an event.
在android 2.3中工作正常,但在4.0中>不会创建事件并引发异常
It work fine in android 2.3, but in 4.0 > it do not creates the event and throws an exception
我不知道我在做什么错,这是我的代码:
I dont know what i am doing wrong, this is my code:
public void addReminder() {
Calendar beginTime = Calendar.getInstance();
long startMillis = beginTime.getTimeInMillis();
long endMillis = beginTime.getTimeInMillis() + 60 * 60 * 1000;
String eventUriString = "content://com.android.calendar/events";
ContentValues eventValues = new ContentValues();
eventValues.put(Events.CALENDAR_ID, 1);
eventValues.put(Events.TITLE, "Test");
eventValues.put(Events.DESCRIPTION, "Test");
eventValues.put(Events.EVENT_TIMEZONE, "Montevideo");
eventValues.put(Events.DTSTART, startMillis);
eventValues.put(Events.DTEND, endMillis);
eventValues.put("eventStatus", 1);
eventValues.put(Events.HAS_ALARM, 1);
Uri eventUri = getContentResolver().insert(
Uri.parse(eventUriString), eventValues);
long eventID = Long.parseLong(eventUri.getLastPathSegment());
String reminderUriString = "content://com.android.calendar/reminders";
ContentValues reminderValues = new ContentValues();
reminderValues.put("event_id", eventID);
reminderValues.put("minutes", 1);
reminderValues.put("method", 1);
Uri reminderUri = getContentResolver().insert(
Uri.parse(reminderUriString), reminderValues);
}
在android 4.0上>这是我得到的:
On android 4.0 > this is what I get:
09-14 14:39:12.118: E/AndroidRuntime(826): FATAL EXCEPTION: main
09-14 14:39:12.118: E/AndroidRuntime(826): android.database.sqlite.SQLiteException
09-14 14:39:12.118: E/AndroidRuntime(826): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:181)
09-14 14:39:12.118: E/AndroidRuntime(826): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
09-14 14:39:12.118: E/AndroidRuntime(826): at android.content.ContentProviderProxy.insert(ContentProviderNative.java:420)
09-14 14:39:12.118: E/AndroidRuntime(826): at android.content.ContentResolver.insert(ContentResolver.java:866)
09-14 14:39:12.118: E/AndroidRuntime(826): at com.example.notificationtest.MainActivity.addReminder(MainActivity.java:131)
09-14 14:39:12.118: E/AndroidRuntime(826): at com.example.notificationtest.MainActivity$2.onClick(MainActivity.java:68)
09-14 14:39:12.118: E/AndroidRuntime(826): at android.view.View.performClick(View.java:4204)
09-14 14:39:12.118: E/AndroidRuntime(826): at android.view.View$PerformClick.run(View.java:17355)
09-14 14:39:12.118: E/AndroidRuntime(826): at android.os.Handler.handleCallback(Handler.java:725)
09-14 14:39:12.118: E/AndroidRuntime(826): at android.os.Handler.dispatchMessage(Handler.java:92)
09-14 14:39:12.118: E/AndroidRuntime(826): at android.os.Looper.loop(Looper.java:137)
09-14 14:39:12.118: E/AndroidRuntime(826): at android.app.ActivityThread.main(ActivityThread.java:5041)
09-14 14:39:12.118: E/AndroidRuntime(826): at java.lang.reflect.Method.invokeNative(Native Method)
09-14 14:39:12.118: E/AndroidRuntime(826): at java.lang.reflect.Method.invoke(Method.java:511)
09-14 14:39:12.118: E/AndroidRuntime(826): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-14 14:39:12.118: E/AndroidRuntime(826): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-14 14:39:12.118: E/AndroidRuntime(826): at dalvik.system.NativeStart.main(Native Method)
这是我的权限:
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
那会是什么?
推荐答案
检查您创建的事件的日历ID是否指向现有的已启用日历(不仅仅是eventValues.put(Events.CALENDAR_ID,1);).尝试添加指向事件的提醒时出现了这样的异常(reminderValues.put("event_id",eventID);),该事件是与与现有日历无关的calendar_id添加的.可能需要检查一下...
Check that calendar id for event you create is pointing to existing enabled calendar (not just eventValues.put(Events.CALENDAR_ID, 1);) . I was getting such exception when trying to add reminder pointing to event (reminderValues.put("event_id", eventID);), which was added with calendar_id not associated with existing calendar. There is probably some check for it...
这篇关于无法在Android 4.0上设置日历事件>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!