android - 编辑/删除谷歌日历事件并获取事件 ID

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

我正在尝试使用 Calendar Provider 编辑和删除 Google 日历中的 Activity 。我已经使用 Calendar Provider 创建了 Activity 。

这是我创建事件的代码:

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

Activity 创建成功。现在我想编辑/删除事件。那么,我该如何执行此操作。对于编辑/删除,我需要创建事件的事件 ID,我怎么才能得到它??

请...帮帮我,伙计们。

最佳答案

检查这个网址

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

关于android - 编辑/删除谷歌日历事件并获取事件 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23626240/

相关文章:

java - 从共享首选项中检索数据

android - 如何启动预填了 "to"字段的电子邮件应用程序?

android - 未触发 ActivityNotFoundException 打开下载的 PDF 文件

java - 具有 map 属性的 Parcelable 嵌套类会导致应用程序崩溃

javascript - 谷歌日历事件不显示到fullcalendar本地主机

google-calendar-api - Google 日历无法识别来自 iCalendars (.ics) 的 VALARM

android - 使用 FLAG_ACTIVITY_NO_HISTORY 和 startActivityForResult 在 Android 应用程序中导航

android - 在我的布局文件中使用 CardView 和 RecyclerView 会引发异常

php - 谷歌日历 API : Selecting/Creating calendars?

android - 这里 map API 错误 : Cannot initialize Map Fragment UNKNOWN