从设备的所有帐户获取活动,仅查看今天的活动

时间:2014-02-02 12:49:07

标签: android calendar android-calendar

我今天需要获取日历上的活动数量。只有所有日历的事件数量,不需要更多。 我发现了几个关于它的线索,但我无法获得今天的事件数量。

使用此代码,变量“events”为我提供第一个日历中所有事件的数量。

int events=0;

    public void read() {

        Context context = getApplicationContext();    
        ContentResolver contentResolver = context.getContentResolver();  

        long ntime = System.currentTimeMillis();  

        Cursor cursr = contentResolver.query(Uri.parse("content://com.android.calendar/events"), new String[]{ "calendar_id", "title", "description", "dtstart", "dtend", "eventLocation" }, "calendar_id=1", null, null);       
        cursr.moveToFirst();  
        String[] CalNames = new String[cursr.getCount()];  
        int[] CalIds = new int[cursr.getCount()];  
        for (int i = 0; i < CalNames.length; i++) {  
            CalIds[i] = cursr.getInt(0);             
            CalNames[i] = "Event"+cursr.getInt(0)+": \nTitle: "+ cursr.getString(1)+"\nDescription: "+cursr.getString(2)+"\nStart Date: "+cursr.getLong(3)+"\nEnd Date : "+cursr.getLong(4)+"\nLocation : "+cursr.getString(5);  
            long StartTime = cursr.getLong(3);  
            long EndTime = cursr.getLong(4);  

            if ((StartTime<ntime)&&(ntime<EndTime)) {  
                System.out.println("In the middle of something");  
                break;  
            }                  
            cursr.moveToNext();  
            events++;
        }  
        cursr.close(); 
        Log.i("control","vueltas:"+events);
        events=0;
    }

我需要修改代码以从设备的所有日历中读取事件,并且仅修改今天的事件。 几天寻找怎么做,但我找到的解决方案(包括这个页面)不知道如何应用我的代码... 这与我的问题非常相似,但没有答案https://stackoverflow.com/questions/18301870/get-todays-calendar-events

感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

您需要做的是查询日历,并为每个日历查询事件实例并计算它们。 像这样的东西

// Projection array. Creating indices for this array instead of doing
// dynamic lookups improves performance.
private static final String[] CALENDAR_PROJECTION = new String[] { Calendars.ACCOUNT_NAME };
private static final String[] INSTANCE_PROJECTION = new String[] { Instances._ID };

// The indices for the projection array above.
private static final int PROJECTION_ACCOUNT_NAME_INDEX = 0;

// this is the method that returns the count of all events for all the calendars
public static int getAllEventsCount(final Context context)
{
    int eventsCount = 0;

    // queries the calendars
    final Cursor calendarCursor = context.getContentResolver().query(Calendars.CONTENT_URI, CALENDAR_PROJECTION, null, null, null);
    while (calendarCursor.moveToNext())
    {
        // gets the calendar name - you need that to query the instances for each calendar
        final String accountName = calendarCursor.getString(PROJECTION_ACCOUNT_NAME_INDEX);
        eventsCount = eventsCount + getCalendarEventsCount(context, accountName);
    }
    calendarCursor.close();
    return eventsCount;
}

// this method gets a count of the events in a given calendar
private static int getCalendarEventsCount(final Context context, final String accountName)
{
    final Calendar beginTime = Calendar.getInstance();
    final Calendar endTime = Calendar.getInstance();

    // get events from the start of the day until the same evening.
    beginTime.set(beginTime.get(Calendar.YEAR), beginTime.get(Calendar.MONTH), beginTime.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
    endTime.set(beginTime.get(Calendar.YEAR), beginTime.get(Calendar.MONTH), beginTime.get(Calendar.DAY_OF_MONTH), 23, 59, 59);

    final long startMillis = beginTime.getTimeInMillis();
    final long endMillis = endTime.getTimeInMillis();

    // Construct the query with the desired date range.
    final Uri.Builder builder = Instances.CONTENT_URI.buildUpon();
    ContentUris.appendId(builder, startMillis);
    ContentUris.appendId(builder, endMillis);

    final String selection = "(" + Calendars.ACCOUNT_NAME + " = ?)";
    final String[] selectionArgs = new String[] { accountName };
    final Cursor cursor = context.getContentResolver().query(builder.build(), INSTANCE_PROJECTION, selection, selectionArgs, Instances.BEGIN + " ASC");
    int count = cursor.getCount();
    cursor.close();
    return count;
}

这已经过测试并且有效。 希望它有所帮助。

答案 1 :(得分:0)

使用Instances.CONTENT_BY_DAY_URI获取特定日期的事件。您可以在此处找到此类查询的示例 - https://github.com/CyanogenMod/android_packages_apps_Calendar/blob/cm-12.0/src/com/android/calendar/Event.java#L307

答案 2 :(得分:-1)

你看过Reading all of today's events using CalendarContract - Android 4.0+了吗?但您仍需要分别查询每个日历。您的代码正在查询所有事件和后处理,而您应该在查询中过滤开始和结束时间。

内容解析器旨在返回内容,而不是只返回计数的通用SQL。为了提高效率,您只需要查询id列并获取游标行数。

Cursor.getCount()