获取Android 系统日程的重复日程的实例 demo
时间: 2024-03-15 18:03:25 浏览: 144
以下是一个获取Android系统日历中重复日程的实例demo:
```java
public class MainActivity extends AppCompatActivity {
private static final String[] EVENT_PROJECTION = new String[]{
CalendarContract.Events._ID,
CalendarContract.Events.TITLE,
CalendarContract.Events.DESCRIPTION,
CalendarContract.Events.EVENT_LOCATION,
CalendarContract.Events.DTSTART,
CalendarContract.Events.DTEND,
CalendarContract.Events.RRULE
};
private static final int PROJECTION_ID_INDEX = 0;
private static final int PROJECTION_TITLE_INDEX = 1;
private static final int PROJECTION_DESCRIPTION_INDEX = 2;
private static final int PROJECTION_LOCATION_INDEX = 3;
private static final int PROJECTION_DTSTART_INDEX = 4;
private static final int PROJECTION_DTEND_INDEX = 5;
private static final int PROJECTION_RRULE_INDEX = 6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Cursor cur = null;
ContentResolver cr = getContentResolver();
// 查询条件
String selection = CalendarContract.Events.DTSTART + ">=? AND " + CalendarContract.Events.DTEND + "<=?";
String[] selectionArgs = new String[]{String.valueOf(System.currentTimeMillis()), String.valueOf(System.currentTimeMillis() + DateUtils.YEAR_IN_MILLIS)};
// 查询系统日历表
cur = cr.query(CalendarContract.Events.CONTENT_URI, EVENT_PROJECTION, selection, selectionArgs, CalendarContract.Events.DTSTART + " ASC");
if (cur != null && cur.getCount() > 0) {
while (cur.moveToNext()) {
long eventID = cur.getLong(PROJECTION_ID_INDEX);
String title = cur.getString(PROJECTION_TITLE_INDEX);
String description = cur.getString(PROJECTION_DESCRIPTION_INDEX);
String location = cur.getString(PROJECTION_LOCATION_INDEX);
long dtstart = cur.getLong(PROJECTION_DTSTART_INDEX);
long dtend = cur.getLong(PROJECTION_DTEND_INDEX);
String rrule = cur.getString(PROJECTION_RRULE_INDEX);
// 判断是否是重复日程
if (!TextUtils.isEmpty(rrule)) {
// 获取重复日程的实例
Uri.Builder builder = CalendarContract.Instances.CONTENT_URI.buildUpon();
ContentUris.appendId(builder, dtstart);
ContentUris.appendId(builder, dtend);
Cursor curInstances = cr.query(builder.build(), EVENT_PROJECTION, null, null, null);
if (curInstances != null && curInstances.getCount() > 0) {
while (curInstances.moveToNext()) {
long instanceID = curInstances.getLong(PROJECTION_ID_INDEX);
String instanceTitle = curInstances.getString(PROJECTION_TITLE_INDEX);
String instanceDescription = curInstances.getString(PROJECTION_DESCRIPTION_INDEX);
String instanceLocation = curInstances.getString(PROJECTION_LOCATION_INDEX);
long instanceDtstart = curInstances.getLong(PROJECTION_DTSTART_INDEX);
long instanceDtend = curInstances.getLong(PROJECTION_DTEND_INDEX);
Log.d("MainActivity", "重复日程实例ID:" + instanceID);
Log.d("MainActivity", "重复日程实例标题:" + instanceTitle);
Log.d("MainActivity", "重复日程实例描述:" + instanceDescription);
Log.d("MainActivity", "重复日程实例地点:" + instanceLocation);
Log.d("MainActivity", "重复日程实例开始时间:" + new Date(instanceDtstart));
Log.d("MainActivity", "重复日程实例结束时间:" + new Date(instanceDtend));
}
}
if (curInstances != null) {
curInstances.close();
}
}
}
}
if (cur != null) {
cur.close();
}
}
}
```
这个示例使用了 `CalendarContract.Instances` 表来获取重复日程的实例。通过查询系统日历表,如果某个事件的 `RRULE` 不为空,则表示该事件是重复事件,然后通过该事件的开始结束时间来查询 `CalendarContract.Instances` 表,获取重复事件的实例。最后遍历实例列表,打印出每个实例的信息。
阅读全文