以编程方式在android日历中创建带有规则的事件

时间:2015-05-08 11:37:23

标签: android android-calendar

我在android日历中创建了一个具有重复规则的事件。代码如下: -

public long createEvents_Usertimelineview()
{
    ContentResolver cr = getContentResolver();
    Calendar calendar = Calendar.getInstance(Locale.getDefault());
    long calID = 11;
    long startMillis = 0; 
    long endMillis = 0;

    String startdate_arr[]= {"2015", "05", "12"};
    String starttime_arr[]={"14", "00", "00"};


    calendar.set(Integer.parseInt(startdate_arr[0]),Integer.parseInt(startdate_arr[1])-1,
            Integer.parseInt(startdate_arr[2]),Integer.parseInt(starttime_arr[0]),
            Integer.parseInt(starttime_arr[1]),Integer.parseInt(starttime_arr[2])); // start Time: 00, 01,59

    startMillis = calendar.getTimeInMillis();   


    String  startdate_arr1[]=  {"2015", "05", "12"};
    String  starttime_arr1[]=  {"15", "30", "00"};

    calendar.set(Integer.parseInt(startdate_arr1[0]),Integer.parseInt(startdate_arr1[1])-1,
            Integer.parseInt(startdate_arr1[2]),Integer.parseInt(starttime_arr1[0]),
            Integer.parseInt(starttime_arr1[1]),Integer.parseInt(starttime_arr1[2])); //End Time: 23, 58,00


    endMillis = calendar.getTimeInMillis();

    Calendar calendar2=Calendar.getInstance(Locale.getDefault());
    calendar2.setTimeInMillis(startMillis-endMillis);


    ContentValues values = new ContentValues();
    values.put(CalendarContract.Events.DTSTART, startMillis);
    values.put(CalendarContract.Events.DTEND, endMillis);
    values.put(CalendarContract.Events.TITLE, "My Grand Demo Event");
    values.put(CalendarContract.Events.DESCRIPTION, "");
    values.put(CalendarContract.Events.CALENDAR_ID, calID);
    values.put(CalendarContract.Events.EVENT_TIMEZONE,TimeZone.getDefault().getID());
    values.put(CalendarContract.Events.ALL_DAY, 0);
    values.put(CalendarContract.Events.AVAILABILITY, "usertimelinedata");

    TimeZone timeZone = TimeZone.getDefault();
    values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
    values.put(CalendarContract.Events.RRULE,"FREQ=WEEKLY;COUNT=16;BYDAY=TU");

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

        values.put(CalendarContract.Events.ORGANIZER,"");

    values.put(CalendarContract.Events.EVENT_LOCATION,"");
    Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);

    long eventID = Long.parseLong(uri.getLastPathSegment());
    Log.e("","Event generate id.. "+eventID);

    return eventID;

}

现在,事件创建得非常好。问题是,在Android日历中,此事件仅显示几周,并且在几周后它不会在日历中查看,而根据其标准,它应该显示16次。 活动开始日期为2015年5月12日,永不结束。类似地,对于具有rrule的所有事件也发生相同的问题。只有HTC希望设备以完美的方式显示事件,而问题出现在moto g和三星设备中。请帮忙解决这个问题......! enter image description here

当我刷日历时,此事件已不复存在......

enter image description here

1 个答案:

答案 0 :(得分:2)

尝试设置事件DURATION而不是DTEND。 所以注释这行value.put(CalendarContract.Events.DTEND,endMillis); 添加此行values.put(CalendarContract.Events.DURATION," PT1H"); PT1H表示1小时。它是RFC 545格式。