Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将事件添加到默认日历_Android - Fatal编程技术网

Android 将事件添加到默认日历

Android 将事件添加到默认日历,android,Android,我正在制作一个预约计划应用程序。我在其中记下姓名、约会日期和约会时间,然后当用户单击ok按钮时,我希望将事件添加到默认的google日历中。但是我做不到。谁能告诉我怎么做吗。 提前感谢: 照你说的做了Pravin这是LOG cat说的 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 u

我正在制作一个预约计划应用程序。我在其中记下姓名、约会日期和约会时间,然后当用户单击ok按钮时,我希望将事件添加到默认的google日历中。但是我做不到。谁能告诉我怎么做吗。 提前感谢: 照你说的做了Pravin这是LOG cat说的

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.

简单的方法如下:-

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" />

我以前也试过,这次也在试。但每次我单击按钮时,它都会说,在添加事件之前,您必须添加至少一个日历帐户,然后我添加了该帐户,但它会再次显示相同的内容。我正在尝试在emulator上运行它。您认为它在android手机上运行良好,而在emulator上运行不好吗。请给我提个建议。谢谢你的建议现在是强制关闭当我点击按钮这是日志猫说的:首先检查是否有任何日历注册任何帐户或没有。非常感谢pravin。。。你的第一个回复是有效的,只是它在emulator上不起作用,但在我的android手机上起作用。