如果Android日历提供程序中的事件未创建或未显示,请检查以下代码示例解决方法:
// 检查是否有读写日历的权限
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
// 请求权限
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CALENDAR}, REQUEST_CODE);
}
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.CALENDAR_ID, calID);
values.put(CalendarContract.Events.TITLE, "Event Title");
values.put(CalendarContract.Events.DESCRIPTION, "Event Description");
values.put(CalendarContract.Events.EVENT_LOCATION, "Event Location");
values.put(CalendarContract.Events.DTSTART, startMillis);
values.put(CalendarContract.Events.DTEND, endMillis);
values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
String[] projection = new String[]{CalendarContract.Events._ID, CalendarContract.Events.TITLE, CalendarContract.Events.DESCRIPTION};
String selection = CalendarContract.Events.CALENDAR_ID + " = ?";
String[] selectionArgs = new String[]{String.valueOf(calID)};
Cursor cursor = cr.query(CalendarContract.Events.CONTENT_URI, projection, selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
do {
long eventID = cursor.getLong(cursor.getColumnIndex(CalendarContract.Events._ID));
String title = cursor.getString(cursor.getColumnIndex(CalendarContract.Events.TITLE));
String description = cursor.getString(cursor.getColumnIndex(CalendarContract.Events.DESCRIPTION));
// 处理事件数据
} while (cursor.moveToNext());
cursor.close();
}
请确保在使用这些代码示例之前,已经获得了适当的权限,并将变量calID、startMillis和endMillis替换为相应的值。这些示例代码应该能够创建和显示Android日历提供程序中的事件。