我正在尝试构建一个简单的示例应用程序,仅计算今天特定日历(我知道日历的 CALENDAR_DISPLAY_NAME)上的事件数。
我认为我需要使用 CalendarContract 查询今天的事件,然后计算游标中的行数?
这是正确的吗?在单个日历上执行查询并让它仅返回尽可能小的数据集(仅事件 ID?)的最小、最有效的方法是什么?
最佳答案
这是我得出的解决方案:
首先从已知名称中找到日历 ID。
然后:
private Cursor getEventInstancesCursor() { // Query for a list of GQueues Calendar instances today
// Set Calendar variable for start and end of today.
Calendar startOfDay = Calendar.getInstance();
int thisYear = startOfDay.get(Calendar.YEAR);
int thisMonth = startOfDay.get(Calendar.MONTH);
int thisDay = startOfDay.get(Calendar.DAY_OF_MONTH);
startOfDay.set(thisYear, thisMonth, thisDay, 0, 0, 0);
long startMillis = startOfDay.getTimeInMillis();
Calendar endOfDay = Calendar.getInstance();
endOfDay.set(thisYear, thisMonth, thisDay, 23, 59, 59);
long endMillis = endOfDay.getTimeInMillis();
long GQueuesCalID = getGQueuesCalID();
Cursor eventsCursor = null;
ContentResolver eventsCR = getContentResolver();
String selection = CalendarContract.Instances.CALENDAR_ID + " = ?";
String calIDString = String.valueOf(GQueuesCalID);
String[] selectionArgs = {calIDString};
try {
return eventsCursor = eventsCR.query(
CalendarContract.Instances.CONTENT_URI.buildUpon()
.appendPath(Long.toString(startMillis))
.appendPath(Long.toString(endMillis))
.build(),
InstancesQuery.PROJECTION,
selection,
selectionArgs,
null);
} catch (Exception e) {
Log.e(TAG, "Error querying calendar API", e);
return null;
}
}
然后只计算光标行数。
关于java - 如何计算特定日历上仅今天的事件数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21677949/