Android使用日期范围从日历中获取事件

时间:2014-04-03 08:54:49

标签: android events android-calendar

实际上,我需要获取周,月,所选日期和今天的日历活动。我成功了一周和一个月,但没有在选定的一天。

String mainDate[] = dayValue.split("-");
            int year, month, day;

            year = Integer.parseInt(mainDate[0]);
            month = Integer.parseInt(mainDate[1]);
            day = Integer.parseInt(mainDate[2]);
Calendar calendar = Calendar.getInstance();
            calendar.set(year, month - 1, day, 0, 0, 0);
long after = calendar.getTimeInMillis();

            SimpleDateFormat formatterr = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

            Calendar endOfDay = Calendar.getInstance();
Cursor cursor;

            endOfDay.set(year, month - 1, day, 22, 59, 59);

            Uri.Builder builder = Uri.parse("content://com.android.calendar/instances/when").buildUpon();
            long now = new Date().getTime();
            ContentUris.appendId(builder, after);
            ContentUris.appendId(builder, endOfDay.getTimeInMillis());

            String[] projection = new String[] { "event_id", "title", "description", "dtstart", "dtend", "eventLocation", "accessLevel", "allDay", Instances.BEGIN, Instances.END };

            String sortOrder = "startDay ASC, startMinute ASC";

            cursor = getActivity().getContentResolver().query(builder.build(), projection, null, null, sortOrder);

//提前,相同的代码,用于循环和获取数据。

现在,问题是,即如果我只获得第14个可能的事件,那么我可以获得第13个可能事件,并且该事件来自facebook及其全天活动。所以我不需要那个活动,我只需要第14次活动。

请建议我。

1 个答案:

答案 0 :(得分:0)

CalendarContract.Instances中有一个内置的query()方法。希望它适用于您的场景。

Date startDate= Create an object of date with the your startdate range; // in ur case may be 14thMay
Date endDate = Create an object of date with the your enddate range; // in ur case may be 14thMay
long beginMillis = startDate.getTime() ;//the number of milliseconds since Jan. 1, 1970, midnight GMT.  
    long endMillis = endDate.getTime() ;
Cursor cursor = CalendarContract.Instances.query(contentResolver, projection, beginMillis, endMillis);