Android 如何获取日历事件实例?安卓
我想从日历中获取接下来7天的活动。我按照下面的步骤编写代码。我已经提前在日历中添加了两个事件。因为它不工作,所以我将while循环更改为if语句。所以我知道光标中没有任何内容。如何通过日历提供程序获取事件实例??有什么帮助吗?多谢各位 这是我的密码:Android 如何获取日历事件实例?安卓,android,android-calendar,Android,Android Calendar,我想从日历中获取接下来7天的活动。我按照下面的步骤编写代码。我已经提前在日历中添加了两个事件。因为它不工作,所以我将while循环更改为if语句。所以我知道光标中没有任何内容。如何通过日历提供程序获取事件实例??有什么帮助吗?多谢各位 这是我的密码: Date date = new Date(); int year=Integer.parseInt(new SimpleDateFormat("yyyy").format(date));
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!!!!!!!!!!!!" );
}