Android提醒意图

时间:2016-04-29 16:14:32

标签: java android android-intent reminders

是否可以使用意图打开创建提醒 一个事件? 我无法找到任何解决方案。

Intent intent = new Intent(Intent.ACTION_INSERT)
        .setData(CalendarContract.Events.CONTENT_URI);
startActivity(intent);

我想调用它:

1 个答案:

答案 0 :(得分:0)

您可以像这样实现它。你需要做的就是调用这个方法,它将负责其余的工作。

public static void pushAppointmentsToCalender(Activity curActivity, 
                                              String title, long startDate) {

    Intent intent = new Intent(Intent.ACTION_EDIT);
    intent.setType("vnd.android.cursor.item/event");
    intent.putExtra("beginTime", startDate);
    intent.putExtra("allDay", true);
    intent.putExtra("endTime", startTime+24*60*60*1000);
    intent.putExtra(CalendarContract.Events.TITLE, title);
    curActivity.startActivity(intent);
}

但在此之前,请确保您已显示这些权限。

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

如果您使用的是Android API 23(6.0)或更高版本,我建议您阅读API 23 here中的新权限模型。并确保在运行时请求,这将有助于您的应用程序支持未来版本的Android。

<强>更新

您应按Sanjeev Kumar in here

的建议尝试此操作
 /** Adds Events and Reminders in Calendar. */
 private void addReminderInCalendar() {
     Calendar cal = Calendar.getInstance();
     Uri EVENTS_URI = Uri.parse(getCalendarUriBase(true) + "events");
     ContentResolver cr = getContentResolver();
     TimeZone timeZone = TimeZone.getDefault();

     /** Inserting an event in calendar. */
     ContentValues values = new ContentValues();
     values.put(CalendarContract.Events.CALENDAR_ID, 1);
     values.put(CalendarContract.Events.TITLE, "Sanjeev Reminder 01");
     values.put(CalendarContract.Events.DESCRIPTION, "A test Reminder.");
     values.put(CalendarContract.Events.ALL_DAY, 0);
     // event starts at 11 minutes from now
     values.put(CalendarContract.Events.DTSTART, cal.getTimeInMillis() + 1 * 60 * 1000);
     // ends 60 minutes from now
     values.put(CalendarContract.Events.DTEND, cal.getTimeInMillis() + 2 * 60 * 1000);
     values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
     values.put(CalendarContract.Events.HAS_ALARM, 1);
     Uri event = cr.insert(EVENTS_URI, values);

     // Display event id.
     Toast.makeText(getApplicationContext(), "Event added :: ID :: " + event.getLastPathSegment(), Toast.LENGTH_SHORT).show();

     /** Adding reminder for event added. */
     Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(true) + "reminders");
     values = new ContentValues();
     values.put(CalendarContract.Reminders.EVENT_ID, Long.parseLong(event.getLastPathSegment()));
     values.put(CalendarContract.Reminders.METHOD, Reminders.METHOD_ALERT);
     values.put(CalendarContract.Reminders.MINUTES, 10);
     cr.insert(REMINDERS_URI, values);
 }

 /** Returns Calendar Base URI, supports both new and old OS. */
 private String getCalendarUriBase(boolean eventUri) {
     Uri calendarURI = null;
     try {
         if (android.os.Build.VERSION.SDK_INT <= 7) {
             calendarURI = (eventUri) ? Uri.parse("content://calendar/") :
             Uri.parse("content://calendar/calendars");
         } else {
             calendarURI = (eventUri) ? Uri.parse("content://com.android.calendar/") : Uri
                     .parse("content://com.android.calendar/calendars");
         }
     } catch (Exception e) {
         e.printStackTrace();
     }
     return calendarURI.toString();
 }