将事件添加到默认日历

时间:2014-02-21 06:26:11

标签: android

我正在预约调度程序应用程序。我在其中取名,约会日期和约会时间,然后当用户点击确定按钮时,我希望在默认谷歌日历中添加事件。但我无法做任何人请告诉我该怎么做。 提前致谢: 做了你的建议Pravin这是LOG猫说的话

02-21 07:40:33.525: D/AndroidRuntime(1510): Shutting down VM
02-21 07:40:33.525: W/dalvikvm(1510): threadid=1: thread exiting with uncaught exception (group=0xb4a84ba8)
02-21 07:40:33.605: E/AndroidRuntime(1510): FATAL EXCEPTION: main
02-21 07:40:33.605: E/AndroidRuntime(1510): Process: com.example.scheduler, PID: 1510
02-21 07:40:33.605: E/AndroidRuntime(1510): java.lang.IllegalArgumentException: Unknown URL nullevents
02-21 07:40:33.605: E/AndroidRuntime(1510):     at android.content.ContentResolver.insert(ContentResolver.java:1186)
02-21 07:40:33.605: E/AndroidRuntime(1510):     at com.example.scheduler.Alert$2.onClick(Alert.java:85)
02-21 07:40:33.605: E/AndroidRuntime(1510):     at android.view.View.performClick(View.java:4438)
02-21 07:40:33.605: E/AndroidRuntime(1510):     at android.view.View$PerformClick.run(View.java:18422)
02-21 07:40:33.605: E/AndroidRuntime(1510):     at android.os.Handler.handleCallback(Handler.java:733)
02-21 07:40:33.605: E/AndroidRuntime(1510):     at android.os.Handler.dispatchMessage(Handler.java:95)
02-21 07:40:33.605: E/AndroidRuntime(1510):     at android.os.Looper.loop(Looper.java:136)
02-21 07:40:33.605: E/AndroidRuntime(1510):     at android.app.ActivityThread.main(ActivityThread.java:5017)
02-21 07:40:33.605: E/AndroidRuntime(1510):     at java.lang.reflect.Method.invokeNative(Native Method)
02-21 07:40:33.605: E/AndroidRuntime(1510):     at java.lang.reflect.Method.invoke(Method.java:515)
02-21 07:40:33.605: E/AndroidRuntime(1510):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-21 07:40:33.605: E/AndroidRuntime(1510):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-21 07:40:33.605: E/AndroidRuntime(1510):     at dalvik.system.NativeStart.main(Native Method)
02-21 07:40:42.285: I/Choreographer(1541): Skipped 32 frames!  The application may be doing too much work on its main thread.
02-21 07:40:42.705: I/Choreographer(1541): Skipped 114 frames!  The application may be doing too much work on its main thread.
02-21 07:40:42.905: D/gralloc_goldfish(1541): Emulator without GPU emulation detected.

1 个答案:

答案 0 :(得分:0)

简单的方法如下: -

Calendar cal = Calendar.getInstance();              
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", cal.getTimeInMillis());
intent.putExtra("allDay", false);
intent.putExtra("rrule", "FREQ=DAILY");
intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
intent.putExtra("title", "A Test Event from android app");
startActivity(intent);

,这是另一种方式

使用此方法获取日历的引用 (强烈建议不要使用此方法,因为它可能会破坏较新的Android版本):

private String getCalendarUriBase(Activity act) {

    String calendarUriBase = null;
    Uri calendars = Uri.parse("content://calendar/calendars");
    Cursor managedCursor = null;
    try {
        managedCursor = act.managedQuery(calendars, null, null, null, null);
    } catch (Exception e) {
    }
    if (managedCursor != null) {
        calendarUriBase = "content://calendar/";
    } else {
        calendars = Uri.parse("content://com.android.calendar/calendars");
        try {
            managedCursor = act.managedQuery(calendars, null, null, null, null);
        } catch (Exception e) {
        }
        if (managedCursor != null) {
            calendarUriBase = "content://com.android.calendar/";
        }
    }
    return calendarUriBase;
}

以这种方式添加事件和提醒:

// get calendar
Calendar cal = Calendar.getInstance();     
Uri EVENTS_URI = Uri.parse(getCalendarUriBase(this) + "events");
ContentResolver cr = getContentResolver();

// event insert
ContentValues values = new ContentValues();
values.put("calendar_id", 1);
values.put("title", "Reminder Title");
values.put("allDay", 0);
values.put("dtstart", cal.getTimeInMillis() + 11*60*1000); // event starts at 11 minutes from now
values.put("dtend", cal.getTimeInMillis()+60*60*1000); // ends 60 minutes from now
values.put("description", "Reminder description");
values.put("visibility", 0);
values.put("hasAlarm", 1);
Uri event = cr.insert(EVENTS_URI, values);

// reminder insert
Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(this) + "reminders");
values = new ContentValues();
values.put( "event_id", Long.parseLong(event.getLastPathSegment()));
values.put( "method", 1 );
values.put( "minutes", 10 );
cr.insert( REMINDERS_URI, values );

您还需要将此权限添加到此方法的清单中:

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