如何日历事件添加到默认日历,默默无意图,在安卓4.0? [英] How to add calendar events to default calendar, silently without Intent, in android 4?

查看:288
本文介绍了如何日历事件添加到默认日历,默默无意图,在安卓4.0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望以编程方式(直接)添加日历事件中的Andr​​oid 4+。难道这可能在仿真器进行测试?我没有自己的android手机。一些示例code将AP preciated。我读的Andr​​oid开发者日历供应商,但我很困惑。我如何添加事件到用户的默认日历?我并不需要进行同步。

I want to add calendar events programmatically (directly) in android 4+. Is it this possible to be tested on emulator? I don't own an android phone. Some sample code would be appreciated. I read Calendar Provider of android developers but I'm confused. How can I add events to the default calendar of a user? I don't need to be synced.

编辑:我不想发动的事件添加意向。相反,我想从code完全加入他们,而不是推出另一项活动。我需要能够测试上的事件将被添加到所述装置的缺省用户的主日历的仿真器。我如何建立一个仿真器来查看用户的默认日历?

I do not want to launch an event adding Intent. Instead I want to add them completely from code and not launch another activity. I need to be able to test on an emulator that the events will be added to the main calendar of the default user of the device. How do I set up an emulator to view the default calendar of the user?

推荐答案

<一个href="http://stackoverflow.com/questions/13652168/how-to-add-calendar-event-with-reminder-repeated-every-day-until-specific-day">Here是什么,我最终使它成为工作的例子:

Here is a working example of what i eventually made it:

 ContentResolver cr = ctx.getContentResolver();
        ContentValues values = new ContentValues();

        values.put(CalendarContract.Events.DTSTART, dtstart);
        values.put(CalendarContract.Events.TITLE, title);
        values.put(CalendarContract.Events.DESCRIPTION, comment);

        TimeZone timeZone = TimeZone.getDefault();
        values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());

        // default calendar
        values.put(CalendarContract.Events.CALENDAR_ID, 1);

        values.put(CalendarContract.Events.RRULE, "FREQ=DAILY;UNTIL="
                + dtUntill);
        //for one hour
        values.put(CalendarContract.Events.DURATION, "+P1H");

        values.put(CalendarContract.Events.HAS_ALARM, 1);

        // insert event to calendar
        Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);

其中,dtuntil为

where dtuntil is

    SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
    Calendar dt = Calendar.getInstance();

    //where untilDate is a date instance of your choice,for example 30/01/2012
    dt.setTime(untilDate);

    //if you want the event until 30/01/2012 we add one day from our day because UNTIL in RRule sets events Before the last day want for event
    dt.add(Calendar.DATE, 1);
    String dtUntill = yyyyMMdd.format(dt.getTime());

参见重复规则

这篇关于如何日历事件添加到默认日历,默默无意图,在安卓4.0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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