使用 'Intent.putExtra()' 方式将带有提醒的事件添加到日历

Adding event with reminders to calendar with 'Intent.putExtra()' way of doing

我正在尝试使用以下代码将事件添加到日历中:

public Intent calPopulation()
{
        Intent calIntent = new Intent(Intent.ACTION_INSERT);
        calIntent.setType("vnd.android.cursor.item/event");
        calIntent.putExtra(CalendarContract.Events.TITLE, this._title);

        GregorianCalendar calDate = new GregorianCalendar(this._year,this._month, this._day, this._hour, this._minute);
        calIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, calDate.getTimeInMillis());
        calIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, calDate.getTimeInMillis()+60*60*1000);
        calIntent.putExtra(CalendarContract.Events.HAS_ALARM, true);
        calIntent.putExtra(CalendarContract.Reminders.EVENT_ID, CalendarContract.Events._ID);
        calIntent.putExtra(CalendarContract.Events.ALLOWED_REMINDERS, "METHOD_DEFAULT");
        calIntent.putExtra(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);

        calIntent.putExtra(CalendarContract.Reminders.MINUTES,5);
        return calIntent;
}

然后启动操作:startActivity(mTask.calPopulation());

我没有任何问题,日历应用程序事件启动时使用了我在我的应用程序中输入的正确信息,只是它没有填写我想添加的提醒事件。

你有什么线索吗?我尝试使用这种方法在许多线程中搜索(我的意思是 intent.putExtra),但从未找到任何有趣的东西。

另外一点,有没有办法不用打开日历App请求用户操作,直接将事件+提醒保存到日历中?

提前致谢。 亚历克斯

按照我指出的方法没有答案,但是我找到了另一种方法可以将带有提醒的事件添加到日历中。

以下方法对我来说效果很好:

// Add an event to the calendar of the user.
    public void addEvent(Context context) {
        GregorianCalendar calDate = new GregorianCalendar(this._year, this._month, this._day, this._hour, this._minute);

        try {
            ContentResolver cr = context.getContentResolver();
            ContentValues values = new ContentValues();
            values.put(CalendarContract.Events.DTSTART, calDate.getTimeInMillis());
            values.put(CalendarContract.Events.DTEND, calDate.getTimeInMillis()+60*60*1000);
            values.put(CalendarContract.Events.TITLE, this._title);
            values.put(CalendarContract.Events.CALENDAR_ID, 1);
            values.put(CalendarContract.Events.EVENT_TIMEZONE, Calendar.getInstance()
                    .getTimeZone().getID());
            System.out.println(Calendar.getInstance().getTimeZone().getID());
            Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);

            // Save the eventId into the Task object for possible future delete.
            this._eventId = Long.parseLong(uri.getLastPathSegment());
            // Add a 5 minute, 1 hour and 1 day reminders (3 reminders)
            setReminder(cr, this._eventId, 5);
            setReminder(cr, this._eventId, 60);
            setReminder(cr, this._eventId, 1440);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // routine to add reminders with the event
    public void setReminder(ContentResolver cr, long eventID, int timeBefore) {
        try {
            ContentValues values = new ContentValues();
            values.put(CalendarContract.Reminders.MINUTES, timeBefore);
            values.put(CalendarContract.Reminders.EVENT_ID, eventID);
            values.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
            Uri uri = cr.insert(CalendarContract.Reminders.CONTENT_URI, values);
            Cursor c = CalendarContract.Reminders.query(cr, eventID,
                    new String[]{CalendarContract.Reminders.MINUTES});
            if (c.moveToFirst()) {
                System.out.println("calendar"
                        + c.getInt(c.getColumnIndex(CalendarContract.Reminders.MINUTES)));
            }
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // function to remove an event from the calendar using the eventId stored within the Task object.
    public void removeEvent(Context context) {
        ContentResolver cr = context.getContentResolver();

        int iNumRowsDeleted = 0;

        Uri eventsUri = Uri.parse(CALENDAR_URI_BASE+"events");
        Uri eventUri = ContentUris.withAppendedId(eventsUri, this._eventId);
        iNumRowsDeleted = cr.delete(eventUri, null, null);

        Log.i(DEBUG_TAG, "Deleted " + iNumRowsDeleted + " calendar entry.");
    }


    public int updateEvent(Context context) {
        int iNumRowsUpdated = 0;
        GregorianCalendar calDate = new GregorianCalendar(this._year, this._month, this._day, this._hour, this._minute);

        ContentValues event = new ContentValues();

        event.put(CalendarContract.Events.TITLE, this._title);
        event.put("hasAlarm", 1); // 0 for false, 1 for true
        event.put(CalendarContract.Events.DTSTART, calDate.getTimeInMillis());
        event.put(CalendarContract.Events.DTEND, calDate.getTimeInMillis()+60*60*1000);

        Uri eventsUri = Uri.parse(CALENDAR_URI_BASE+"events");
        Uri eventUri = ContentUris.withAppendedId(eventsUri, this._eventId);

        iNumRowsUpdated = context.getContentResolver().update(eventUri, event, null,
                null);

        // TODO put text into strings.xml
        Log.i(DEBUG_TAG, "Updated " + iNumRowsUpdated + " calendar entry.");

        return iNumRowsUpdated;
    }

希望这可以帮助遇到与我相同问题的其他人:)。

亚历克斯

同意上述回答,但重要的是日历 ID。您不能使用 1,因为三星 phone 使用 1 作为他们的日历 (S Planner)。所以日历 ID 是您想要事件的电子邮件的 ID。您可以按照特定事件的代码获取日历 ID

int calenderId=-1;
        String calenderEmaillAddress="xxx@gmail.com";
        String[] projection = new String[]{
                CalendarContract.Calendars._ID,
                CalendarContract.Calendars.ACCOUNT_NAME};
        ContentResolver cr = activity.getContentResolver();
        Cursor cursor = cr.query(Uri.parse("content://com.android.calendar/calendars"), projection,
                CalendarContract.Calendars.ACCOUNT_NAME + "=? and (" +
                        CalendarContract.Calendars.NAME + "=? or " +
                        CalendarContract.Calendars.CALENDAR_DISPLAY_NAME + "=?)",
                new String[]{calenderEmaillAddress, calenderEmaillAddress,
                        calenderEmaillAddress}, null);

        if (cursor.moveToFirst()) {

            if (cursor.getString(1).equals(calenderEmaillAddress))
                 calenderId=cursor.getInt(0); //youre calender id to be insered in above  answer


        }