3

寻找我的问题的答案已经有一段时间了。我想要做的:从当天的所有日历中获取所有事件

我可以在 ICS 之前的设备(2.2 到 3.2 Android 版本)上毫无问题地做到这一点。在 Android 4.0+ 上,自从引入了新的日历 API 以来,我一直在处理它并取得了相当大的成功。好吧,这就是我在测试其他设备而不是我的设备之前的想法。有时我的代码工作,有时它不会。重复事件不会出现在某些设备或其他设备上,而是显示所有事件(不仅是当天的事件)。

我认为问题出在一行代码中,但我找不到正确的方法:

    cursor = contentResolver.query(CalendarContract.Events.CONTENT_URI, COLS, "("+CalendarContract.Events.DTSTART+">"+now+" and "+CalendarContract.Events.DTEND+"<"+endOfDay.getTimeInMillis()+") or (("+CalendarContract.Events.ALL_DAY+"=1) and "+"("+CalendarContract.Events.DTSTART+">="+timeAllDay.getTimeInMillis()+") and "+"("+CalendarContract.Events.DTEND+"<="+endTimeAllDay.getTimeInMillis()+"))", null, null);

这是我使用上述行创建的所有类:

    public class ICSCalendar extends AsyncTask<Intent, String, Intent> 
    {
      private static final String[] COLS = new String[] {CalendarContract.Events.TITLE, CalendarContract.Events.DTSTART, CalendarContract.Events.DTEND, CalendarContract.Events.ALL_DAY};

@Override
protected Intent doInBackground(Intent... params) 
{
    ContentResolver contentResolver = Alert.mContext.getContentResolver();
    Cursor cursor = null;

    try
    {
        long now = new Date().getTime();
        Calendar endOfDay = Calendar.getInstance();
        endOfDay.set(Calendar.HOUR_OF_DAY, 23);
        endOfDay.set(Calendar.MINUTE, 59);
        endOfDay.set(Calendar.SECOND, 59);

        Calendar timeAllDay = Calendar.getInstance();
        timeAllDay.set(Calendar.HOUR_OF_DAY, 0);
        timeAllDay.set(Calendar.MINUTE, 0);
        timeAllDay.set(Calendar.SECOND, 0);

        Calendar endTimeAllDay = Calendar.getInstance();
        endTimeAllDay.add(Calendar.DAY_OF_MONTH, 1);
        endTimeAllDay.set(Calendar.HOUR_OF_DAY, 2);
        endTimeAllDay.set(Calendar.MINUTE, 0);
        endTimeAllDay.set(Calendar.SECOND, 1);

        cursor = contentResolver.query(CalendarContract.Events.CONTENT_URI, COLS, "("+CalendarContract.Events.DTSTART+">"+now+" and "+CalendarContract.Events.DTEND+"<"+endOfDay.getTimeInMillis()+") or (("+CalendarContract.Events.ALL_DAY+"=1) and "+"("+CalendarContract.Events.DTSTART+">="+timeAllDay.getTimeInMillis()+") and "+"("+CalendarContract.Events.DTEND+"<="+endTimeAllDay.getTimeInMillis()+"))", null, null);

        Intent calendarData = new Intent(Alerts.CALENDAR_UI_DATA);
        int nb_events = 0;

        while(cursor.moveToNext()) 
        {
            nb_events++;

            final String title = cursor.getString(0);
            final Date begin = new Date(cursor.getLong(1));
            final Boolean allDay = !cursor.getString(3).equals("0");

            calendarData.putExtra("event_"+nb_events, (String) title);
            calendarData.putExtra("all_day_event_"+nb_events, (Boolean) allDay);

            Calendar beginEvent = Calendar.getInstance();   
            beginEvent.setTimeInMillis(cursor.getLong(1));
            String format = Alerts.get24HourMode(Alert.mContext) ? "kk:mm": "h:mm";
            CharSequence newTime = DateFormat.format(format, beginEvent);
            if(format == "h:mm")
            {
                if((beginEvent.get(Calendar.AM_PM) == 0))
                {
                    newTime = newTime + " A.M";
                }
                else
                {
                    newTime = newTime + " P.M";
                }
            }

            if(!allDay)
            {
                calendarData.putExtra("date_event_"+nb_events, newTime);
            }
            else
            {
                calendarData.putExtra("date_event_"+nb_events, Alert.mContext.getString(R.string.tts_whole_day_calendar_event));
            }
        }

        calendarData.putExtra("nb_events", nb_events);

        return calendarData;
    }
    catch(Exception e)
    {
        Log.d("DEBUG", ""+e);
        return null;
    }
}

@Override
protected void onPostExecute(Intent result) 
{
    // TODO Auto-generated method stub
    super.onPostExecute(result);

    try
    {
        Alert.mContext.sendBroadcast(result);
    }
    catch(NullPointerException e){}
}

@Override
protected void onPreExecute() 
{
    // TODO Auto-generated method stub
    super.onPreExecute();
}

@Override
protected void onProgressUpdate(String... values) 
{
    // TODO Auto-generated method stub
    super.onProgressUpdate(values);
}

}

任何人都知道如何从 Android 4.0+ 的当天所有日历中获取所有事件?感谢你的帮助 :)

4

1 回答 1

2

为了获得重复事件,您需要实例表,请在此处查看更多详细信息

这并不像看起来那么容易我害怕

于 2012-12-17T18:29:09.543 回答