在Android 2.2中添加日历和事件

Kha*_*war 7 android uri calendar

我想要的:我想在Android 2.2中添加日历事件.

我有什么:我使用以下代码添加了一个事件

    Uri calendars = Uri.parse("content://com.android.calendar/events");
    Cursor managedCursor = managedQuery(calendars, null, null, null, null);

    startManagingCursor(managedCursor);
    managedCursor.moveToFirst();

    String ID = null;

    do
    {
        ID = managedCursor.getString(managedCursor.getColumnIndexOrThrow("_id"));
    } 
    while (managedCursor.moveToNext());
    managedCursor.close();      

    int NewID = Integer.parseInt(ID) + 1;

    ContentValues event = new ContentValues();
    event.put("calendar_id", NewID);  // --- Some confusion Here with the ID,  
                                      // --- not sure how to use it here
    event.put("title", "New Event Title");
    event.put("description", "Event Desc");
    event.put("eventLocation", "Somewhere");

    long startTime = System.currentTimeMillis() + 1000 * 60 * 60;
    long endTime = System.currentTimeMillis() + 1000 * 60 * 60 * 2;

    event.put("dtstart", startTime);
    event.put("dtend", endTime);

    event.put("allDay", 0); // 0 for false, 1 for true
    event.put("eventStatus", 1);
    event.put("visibility", 0);
    event.put("transparency", 0);
    event.put("hasAlarm", 0); // 0 for false, 1 for true

    Uri eventsUri = Uri.parse("content://com.android.calendar/events");
    Uri insertedUri = getContentResolver().insert(eventsUri, event);
Run Code Online (Sandbox Code Playgroud)

问题是:
到目前为止,我已经成功地在指定的日期时间添加了一个事件,显然NewID的角色对我来说是可疑的.当我尝试添加其他一些事件时,我得到返回的Uri insertedUri,它会在URI的末尾显示新添加的ID.但我无法在设备上看到任何此类事件.可能是我对日历和事件的理解存在一些问题,或者两者及其ID的差异.请指导我错过或做错的事.

此致,
Khawar

Far*_*han 4

大部分代码都很好,您只需要了解一些有关日历的概念即可。实际上Android中有不止一种日历类型。

要遍历所有日历,请使用以下 2.2 的 Uri:

Uri calendars = Uri.parse("content://com.android.calendar"+ "/calendars");
Run Code Online (Sandbox Code Playgroud)

获取'id'和'name'的值,你就会明白了。

NewID's role is suspicious to me
Run Code Online (Sandbox Code Playgroud)

您的插入代码很好,您只需提供要在其中插入任何事件的日历的 ID。

我仍然相信,即使是我自己也需要学习很多东西,所以如果您有什么要告诉或纠正的,我们非常欢迎。以下链接对我有帮助:

使用 Android 日历

不使用 gdata api 访问日历事件