给我个最简单的CalendarContract.Instances.CONTENT_URI的demo
时间: 2024-03-10 14:43:42 浏览: 172
instances_train2017_11000.json
好的,以下是一个最简单的使用CalendarContract.Instances.CONTENT_URI查询当前时间之内的所有日历事件的示例:
```
long now = System.currentTimeMillis();
String[] projection = new String[] {CalendarContract.Instances._ID, CalendarContract.Instances.TITLE, CalendarContract.Instances.BEGIN};
Cursor cursor = getContentResolver().query(CalendarContract.Instances.CONTENT_URI,
projection,
CalendarContract.Instances.BEGIN + " <= ? AND " + CalendarContract.Instances.END + " >= ?",
new String[] {String.valueOf(now), String.valueOf(now)},
null);
if (cursor != null) {
while (cursor.moveToNext()) {
String eventId = cursor.getString(cursor.getColumnIndex(CalendarContract.Instances._ID));
String eventTitle = cursor.getString(cursor.getColumnIndex(CalendarContract.Instances.TITLE));
long eventBeginTime = cursor.getLong(cursor.getColumnIndex(CalendarContract.Instances.BEGIN));
Log.d("CalendarEvent", "Event ID: " + eventId + ", Title: " + eventTitle + ", Begin Time: " + eventBeginTime);
}
cursor.close();
}
```
这个示例中,我们首先获取当前时间,然后指定投影(projection)参数来获取事件的ID、标题和开始时间。接下来,我们使用ContentResolver.query()方法来查询CalendarContract.Instances.CONTENT_URI,并指定开始时间和结束时间的限制条件。最后,我们遍历Cursor并获取每个事件的信息。
阅读全文