如何设置在Android的提醒?如何设置、Android

2023-09-11 12:04:41 作者:悔之晚矣

我想建立为Android日历应用程序。我打在中间。我已成功地检索信息,例如时间和来自用户的任务。

I am trying to build a calendar app for Android. I am struck in the middle. I have managed to retrieve the information such as time and task from the user.

我不知道如何添加到这一机器人的事件。有没有像 SetEvent的或类似的东西?

I don't know how to add this into android events. Is there anything like setEvent or something similar?

推荐答案

不,它不仅仅是调用一个方法,如果要透明地将其添加到用户的日历更加复杂。

Nope, it is more complicated than just calling a method, if you want to transparently add it into the user's calendar.

您已经有了一个几条路;

You've got a couple of choices;

调用的意图,在日历上添加事件 这将弹出的日历应用程序,让用户添加事件。您可以传递一些参数prepopulate领域:

Calling the intent to add an event on the calendar This will pop up the Calendar application and let the user add the event. You can pass some parameters to prepopulate fields:

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);

还是比较复杂的:

Or the more complicated one:

获取一个参考日历用这种方法 (这是高建议的不可以使用此方法,因为它可以打破在较新的Andr​​oid版本):

Get a reference to the calendar with this method (It is highly recommended not to use this method, because it could break on newer Android versions):

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 );

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

You'll also need to add these permissions to your manifest for this method:

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

更新:ICS问题

以上示例使用无证日历的API,新的公共日历API已发布ICS,所以因为这个原因,目标应该使用新的Andr​​oid版本CalendarContract.

The above examples use the undocumented Calendar APIs, new public Calendar APIs have been released for ICS, so for this reason, to target new android versions you should use CalendarContract.

关于这方面更多的相关信息可以在这个博客帖子中。

More infos about this can be found at this blog post.