在Android 2.2中添加日历和事件

发布于 2024-11-11 12:44:40 字数 1742 浏览 0 评论 0原文

我想要什么:我想在 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);

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

问候,
卡瓦尔

What I want: I want to add calendar events in 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. 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. 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. Kindly guide me what I am missing or doing wrong.

Regards,
Khawar

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄居人 2024-11-18 12:44:40

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

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

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

并获取“id”和“name”的值,您就会明白了。

NewID's role is suspicious to me

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

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

使用 Android日历

不使用 gdata 访问日历事件API

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.

To Traverse all the calendars, Use following Uri for 2.2:

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

And get the values of 'id' and 'name', you will get the idea.

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.

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

Accessing Calendars events without using gdata api

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文