如何获取日历事件实例?安卓

问题描述

我想从日历中获取接下来7天的活动。我按照google doc query instance编写代码。我已经提前在日历中添加了两个事件。因为它不起作用,所以我将while循环更改为if语句。所以我知道光标中什么也没有。如何通过日历提供程序获取事件实例?有什么帮助吗?谢谢。

这是我的代码

    Date date = new Date();
        int year=Integer.parseInt(new SimpleDateFormat("yyyy").format(date));
        int month=Integer.parseInt(new SimpleDateFormat("MM").format(date));
        int day=Integer.parseInt(new SimpleDateFormat("dd").format(date));
        int hour=Integer.parseInt(new SimpleDateFormat("hh").format(date));
        int min=Integer.parseInt(new SimpleDateFormat("mm").format(date));

        Date newDate = new Date(date.getTime());

        GregorianCalendar date2 = new GregorianCalendar();
        date2.setTime(newDate);
        date2.add(Calendar.DATE,7);
        newDate.setTime(date2.getTime().getTime());

        int yearafterWeek=Integer.parseInt(new SimpleDateFormat("yyyy").format(newDate));
        int monthAfterWeek=Integer.parseInt(new SimpleDateFormat("MM").format(newDate));
        int dayAfterWeek=Integer.parseInt(new SimpleDateFormat("dd").format(newDate));
        int hourAfterWeek=Integer.parseInt(new SimpleDateFormat("hh").format(newDate));
        int minAfterWeek=Integer.parseInt(new SimpleDateFormat("mm").format(newDate));


        // Specify the date range you want to search for recurring
// event instances
        Calendar beginTime = Calendar.getInstance();
        beginTime.set(year,month,day,hour,min);
        long startMillis = beginTime.getTimeInMillis();
        Calendar endTime = Calendar.getInstance();
        endTime.set(yearafterWeek,monthAfterWeek,dayAfterWeek,hourAfterWeek,minAfterWeek);
        long endMillis = endTime.getTimeInMillis();

        Cursor cur = null;
        ContentResolver cr = context.getContentResolver();

        // The ID of the recurring event whose instances you are searching
// for in the Instances table
        String selection = CalendarContract.Instances.EVENT_ID + " = ?";
        String[] selectionArgs = new String[] {"207"};

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

// Submit the query
        cur =  cr.query(builder.build(),INSTANCE_PROJECTION,selection,null,null);


        if (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()));


        }else{
            Log.d(DEBUG_TAG,"nothing!!!!!!!!!!!!" );
        }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)