简体   繁体   English

在 Android 2.2 中添加日历和事件

[英]Adding Calendar and events in Android 2.2

What I want: I want to add calendar events in Android 2.2.我想要什么:我想在 Android 2.2 中添加日历事件。

What I Have: I have added an event using the below code我所拥有的:我使用以下代码添加了一个事件

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

What is the problem:问题是什么:
So far I have been successful in adding a single event on the specified date time, and apparently NewID 's role is suspicious to me.到目前为止,我已经成功地在指定的日期时间添加了一个事件,显然NewID的角色对我来说是可疑的。 When I try to add some other event, I get the returned Uri insertedUri and it shows me the newly added ID at the end of the URI.当我尝试添加一些其他事件时,我得到了返回的Uri insertUri ,它在 URI 末尾显示了新添加的 ID。 But I cant see any such event on the device.但我在设备上看不到任何此类事件。 May be there is some problem in my understanding of the Calendar and events, or differences in both and their ID's.可能是我对日历和事件的理解存在问题,或者两者及其 ID 存在差异。 Kindly guide me what I am missing or doing wrong.请指导我我错过了什么或做错了什么。

Regards,问候,
Khawar哈瓦尔

Most of the code is fine, you just need to know a little bit of concepts regarding calendar.大部分代码都很好,你只需要知道一点关于日历的概念。 Actually there is more than one calendar types in Android.实际上 Android 中有不止一种日历类型。

To Traverse all the calendars, Use following Uri for 2.2:要遍历所有日历,请在 2.2 中使用以下 Uri:

Uri calendars = Uri.parse("content://com.android.calendar"+ "/calendars");

And get the values of 'id' and 'name', you will get the idea.并得到'id'和'name'的值,你就会明白了。

NewID's role is suspicious to me

Your insertion code is fine, you just need to give the id of that calendar in which you want to insert any event.您的插入代码很好,您只需要提供要在其中插入任何事件的日历的 ID。

I still believe that even myself needs to learn alot so if you have anything to tell or correct, you are most welcome.我仍然相信,即使是我自己也需要学习很多东西,所以如果你有什么要告诉或纠正的,欢迎你。 Following links helped me:以下链接帮助了我:

Working with Android Calendars 使用 Android 日历

Accessing Calendars events without using gdata api 在不使用 gdata api 的情况下访问日历事件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM