【问题标题】:get calendar events for one day into android app将一天的日历事件获取到 android 应用程序中
【发布时间】:2014-05-15 06:26:32
【问题描述】:

我想知道如何从特定日期的日历中获取事件我只想阅读所选日期的事件?

这样读取所有日历事件:

while (cur.moveToNext()) {
    String title = null;
    long eventID = 0;
    long beginVal = 0;    

    // Get the field values
    eventID = cur.getLong(PROJECTION_ID_INDEX);
    beginVal = cur.getLong(PROJECTION_BEGIN_INDEX);
    title = cur.getString(PROJECTION_TITLE_INDEX);

    // Do something with the values. 
    Log.i(DEBUG_TAG, "Event:  " + title); 
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(beginVal);  
    DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
    Log.i(DEBUG_TAG, "Date: " + formatter.format(calendar.getTime()));    
    }
 }

【问题讨论】:

    标签: android android-contentprovider android-calendar


    【解决方案1】:

    您应该在查询中使用selection 按开始日期过滤事件。例如。

    Calendar calendar = Calendar.getInstance();
    calendar.set(2014, Calendar.MAY, 14, 0, 0, 0);
    long startDay = calendar.getTimeInMillis();
    calendar.set(2014, Calendar.MAY, 14, 23, 59, 59);
    long endDay = calendar.getTimeInMillis();
    
    String[] projection = new String[] { BaseColumns._ID, CalendarContract.Events.TITLE, CalendarContract.Events.DTSTART };
    String selection = CalendarContract.Events.DTSTART + " >= ? AND " + CalendarContract.Events.DTSTART + "<= ?";
    String[] selectionArgs = new String[] { Long.toString(startDay), Long.toString(endDay) }; 
    
    Cursor cur = getContentResolver().query(CalendarContract.Events.CONTENT_URI, projection, selection, selectionArgs, null);
    

    【讨论】:

    • 好的,但是如何在 5 月 14 日调用事件.. 我对如何将事件显示到应用程序 textView 有混乱的理解?
    • @kura 对不起,我不明白你的评论。您不想获得一天的活动吗?
    • 使用 CalendarContract.Instances 如果您还想获取重复发生的事件而无需获取所有事件并检查它是否在这一天重复发生
    猜你喜欢
    • 2016-07-04
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多