修改/删除Google日历活动并获取活动ID

时间:2014-05-13 08:29:45

标签: android android-intent google-calendar-api android-contentprovider

我正在尝试使用日历提供程序编辑和删除Google日历中的事件。我已经使用Calendar Provider创建了活动。

以下是我创建活动的代码:

    Calendar beginTime = Calendar.getInstance();
    beginTime.set(2014, 5, 19, 7, 30);
    Calendar endTime = Calendar.getInstance();
    endTime.set(2014, 5, 19, 8, 30);
    Intent intent = new Intent(Intent.ACTION_INSERT)
            .setData(Events.CONTENT_URI)
            .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis())
            .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis())
            .putExtra(Events.TITLE, "")
            .putExtra(Events.DESCRIPTION, "")
            .putExtra(Events.EVENT_LOCATION, "")
            .putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY)
            .putExtra(Intent.EXTRA_EMAIL, email);
    startActivity(intent);

事件已成功创建。现在我想编辑/删除该事件。那么,我该怎么做呢。对于编辑/删除,我需要创建事件的事件ID,我怎么能得到它??

请......帮助我们。

1 个答案:

答案 0 :(得分:6)

检查此网址

Update and delete calendar events in android through my application

希望有所帮助:)

获取活动ID:

private int ListSelectedCalendars(String eventtitle) {


    Uri eventUri;
    if (android.os.Build.VERSION.SDK_INT <= 7) {
        // the old way

        eventUri = Uri.parse("content://calendar/events");
    } else {
        // the new way

        eventUri = Uri.parse("content://com.android.calendar/events");
    }

    int result = 0;
    String projection[] = { "_id", "title" };
    Cursor cursor = getContentResolver().query(eventUri, null, null, null,
            null);

    if (cursor.moveToFirst()) {

        String calName;
        String calID;

        int nameCol = cursor.getColumnIndex(projection[1]);
        int idCol = cursor.getColumnIndex(projection[0]);
        do {
            calName = cursor.getString(nameCol);
            calID = cursor.getString(idCol);

             if (calName != null && calName.contains(eventtitle)) {
            result = Integer.parseInt(calID);
            }

        } while (cursor.moveToNext());
        cursor.close();
    }

    return result;

}

更新活动:

@SuppressLint("InlinedApi")
private int UpdateCalendarEntry(int entryID) {
    int iNumRowsUpdated = 0;

    Uri eventUri;
    if (android.os.Build.VERSION.SDK_INT <= 7) {
        // the old way

        eventUri = Uri.parse("content://calendar/events");
    } else {
        // the new way

        eventUri = Uri.parse("content://com.android.calendar/events");
    }

    ContentValues values = new ContentValues();
    values.put(Events.TITLE, "test");
    values.put(Events.EVENT_LOCATION, "Chennai");

    Uri updateUri = ContentUris.withAppendedId(eventUri, entryID);
    iNumRowsUpdated = getContentResolver().update(updateUri, values, null,
            null);

    return iNumRowsUpdated;
}

删除事件:

    private int DeleteCalendarEntry(int entryID) {
    int iNumRowsDeleted = 0;

    Uri eventUri = ContentUris
            .withAppendedId(getCalendarUriBase(), entryID);
    iNumRowsDeleted = getContentResolver().delete(eventUri, null, null);

    return iNumRowsDeleted;
}

private Uri getCalendarUriBase() {
    Uri eventUri;
    if (android.os.Build.VERSION.SDK_INT <= 7) {
        // the old way

        eventUri = Uri.parse("content://calendar/events");
    } else {
        // the new way

        eventUri = Uri.parse("content://com.android.calendar/events");
    }

    return eventUri;
}