How to add calendar events to default calendar, silently without Intent, in android 4?

后端 未结 6 1160
迷失自我
迷失自我 2020-12-04 16:41

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 wou

6条回答
  •  生来不讨喜
    2020-12-04 17:36

    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);
    // Set Period for 1 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);
    

    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, you must add one day from our day because UNTIL in RRule sets events before the last day
    dt.add(Calendar.DATE, 1);
    String dtUntill = yyyyMMdd.format(dt.getTime());
    

    Ref: Recurrence Rule

提交回复
热议问题