【问题标题】:Edit/Delete Google Calendar Events and get event Id编辑/删除 Google 日历活动并获取活动 ID
【发布时间】:2014-06-30 19:57:12
【问题描述】:

我正在尝试使用日历提供程序编辑和删除 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,我怎样才能得到它??

请...帮帮我。

【问题讨论】:

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


【解决方案1】:

检查这个网址

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

【讨论】:

  • 嗨@Shini,我已经使用你的代码更新事件并且它正在更新但不知道事件正在改变它的一天。例如,如果我在第 13 天创建事件,它在更新事件之后是 12 天。您对此有任何想法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-12
  • 1970-01-01
  • 2022-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多