在 Android 日历中插入多个事件 [英] Insert multiple events in Android calendar

查看:15
本文介绍了在 Android 日历中插入多个事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到解决问题的方法.我正在尝试将多个事件添加到我的 Android 日历中,但我不知道如何.我给找到了这段代码:

I am having trouble in finding a solution to my problem. I am trying to add multiple events into my Android calendar but I do not know how. I gave found this code :

        Calendar cal = Calendar.getInstance();              
            Intent intent = new Intent(Intent.ACTION_EDIT);
            intent.setType("vnd.android.cursor.item/event");
            intent.putExtra("beginTime", cal.getTimeInMillis());
            intent.putExtra("allDay", true);
            intent.putExtra("rrule", "FREQ=YEARLY");
            intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
            intent.putExtra("title", "A Test Event from android app"+String.valueOf(i));
            startActivity(intent);

但它所做的只是将我发送到我的日历并让我编辑和手动插入我的事件.我想自动插入事件,而不是去我的日历应用程序.此外,我只能使用此代码添加一个事件.这不是我想要的.我有 2 个字符串数组,一个包含日期,一个包含事件名称.有谁知道是否有办法做到这一点?我一直在寻找解决方案很长一段时间,但没有找到.如果有人帮助我,我将不胜感激.谢谢!

but all it does is to send me to my calendar and lets me edit and manually insert my event. I want to insert the events automatically, without going to my calendar app. Moreover, I can add only one event using this code. This is not what I wanted. I have 2 String arrays, one containing the date, and one containing the name of the event. Does anyone know if there is a way to do this? I have been searching for a solution for quite a while and hadn't found one. I would be grateful if anyone helped me. Thank you!

我已经解决了这个问题!非常感谢你!但是现在我有另一个我无法解决的问题.我有这个代码:

I have solved the problem! Thank you very much! But now I have another issue that I can't solve. I have this code :

        ContentValues cv = new ContentValues();
        cv.put("calendar_id", calIds[0]);
        cv.put("title", title);
        //cv.put("dtstart", dtstart );
        //cv.put("dtend", dtend);
        cv.put("rrule", "FREQ=MONTHLY");
        cv.put("description", comment );
        Calendar start = Calendar.getInstance();
        start.set(2012, 0, 2, 8, 0, 0);

        Calendar end = Calendar.getInstance();
        end.set(2012, 0, 2, 9, 0, 0);

        long startTime = start.getTimeInMillis();       
        long endTime = end.getTimeInMillis();

        cv.put("dtstart", startTime);
        cv.put("dtend", endTime);
        //Insertion on the events of the calendar
        cr.insert(Uri.parse("content://com.android.calendar/events"), cv);

它会插入我的事件,但不会重复发生.我的意思是我的活动出现在 2jan2012 但在 2jan2013 没有出现,2014 年也没有出现,依此类推.所以我在电话上打开我的日历并尝试编辑我的事件,我看到我应该在哪里选择它的发生,它显示在 2012 年 1 月 2 日而不是 1 月 2 日,因为它应该从我的自定义事件电话.另一方面,如果我尝试从我的手机手动添加一个事件,它工作得很好(我的意思是我可以添加一个每年发生的事件).

It inserts my event but it doesn't do it recurring. I mean that my event appears on 2jan2012 but on 2jan2013 doesn't and neither does in 2014 and so on. So I opened my calendar on the phone and tried to edit my event and I saw that where I should select the occurence of it, there it shows on 2 january 2012 not on 2 january as it should if it were to customize my event from my phone. On the other hand, if I try to add an event manually from my phone it works just fine( I mean I can add an event that occurs yearly).

推荐答案

android SDK 没有暴露任何接口来操作日历,但是你可以从android 源代码中找到.Android 将日历存储在内部 sqlite 数据库中,尽管它受到保护,无法直接访问,但您仍然可以通过 ContentResolver 添加/删除/更新/查询日历数据.插入事件的一段代码可能是这样的:

android SDK does not expose any interfaces to manipulate calendar, but you can find that from android source code. Android stores calendar in an internal sqlite database, though it's protected from direct access but you can still add/delete/update/query calendar data through ContentResolver. A piece of code to insert an event could be like this:

public String addEvent(String calendarId, String title, long startTime,
        long endTime, int allDay) {
    ContentValues event = new ContentValues();
    event.put("calendar_id", calendarId); // "" for insert
    event.put("title", title);
    event.put("description", "");
    event.put("eventLocation", "");
    event.put("allDay", allDay);
    event.put("eventStatus", 1);
    event.put("transparency", 0);
    event.put("dtstart", startTime);
    event.put("dtend", endTime);

    ContentResolver contentResolver = this.context.getContentResolver();
    Uri eventsUri = Uri.parse("content://com.android.calendar/calendars");
    Uri url = contentResolver.insert(eventsUri, event);
    String ret = url.toString();
    return ret;
}

当你插入一个事件成功后,ContentResolver.insert会返回一个代表该事件uri的字符串,你可以在以后查询、更新或删除它.在 SDK 8 之前的早期 SDK 中,日历的内容 uri 是content://calendar/calendars",这与 SDK 8 及之后的版本不同.
同时,请小心那些自定义 rom.由于 SDK 文档中没有注明日历 API,日历提供程序可能会被某些供应商和运营商修改甚至删除,因此您可能需要针对大量设备测试您的应用程序.
祝你好运!

When you insert one event successfully, a string represent uri of the event is returned by ContentResolver.insert, you can query, update or delete it later. In earlier SDKs before SDK 8, the content uri of calendar is "content://calendar/calendars" which differs from SDks 8 and after.
Meanwhile, be careful with those custom roms. since calendar API is not noted in SDK docs, the calendar provider could be modified or even removed by some vendors and operators, so you may have to test your application against lots of devices.
Good luck!

这篇关于在 Android 日历中插入多个事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆