我能够从下面的代码中检索设备日历中的所有事件,
@SuppressLint("MissingPermission")
private fun getAccountCalendarEvents(selectedEmail: String) {
var count = 0
val projectionArray =
arrayOf(
CalendarContract.Calendars._ID,
CalendarContract.Events.TITLE,
CalendarContract.Events.DESCRIPTION,
CalendarContract.Events.DTSTART,
CalendarContract.Events.DTEND,
CalendarContract.Events.ALL_DAY,
CalendarContract.Events.EVENT_LOCATION,
CalendarContract.Attendees.ORGANIZER,
CalendarContract.Events.EVENT_TIMEZONE,
CalendarContract.Events.ACCOUNT_TYPE,
CalendarContract.Calendars.ACCOUNT_NAME
)
val startTime = Calendar.getInstance()
startTime.set(Calendar.HOUR_OF_DAY, 0)
startTime.set(Calendar.MINUTE, 0)
startTime.set(Calendar.SECOND, 0)
startTime.add(Calendar.YEAR, -2)
val endTime = Calendar.getInstance()
endTime.add(Calendar.YEAR, 2)
val selection =
"(( " + CalendarContract.Events.DTSTART + " >= " + startTime.timeInMillis + " ) AND ( " + CalendarContract.Events.DTEND + " <= " + endTime.timeInMillis + " ) AND ( deleted != 1 ) AND (account_name = '" + selectedEmail + "'))"
val cursor = contentResolver.query(
CalendarContract.Events.CONTENT_URI,
projectionArray,
selection,
null,
null
)
if (cursor != null && cursor.count > 0 && cursor.moveToFirst()) {
do {
var calendarString = ""
val calendarId = "Id: " + cursor.getInt(0)
val title = "Title: " + cursor.getString(1)
val description = "Description: " + cursor.getString(2)
val startDate = "Start Date: " + cursor.getString(3)
val endDate = "End Date: " + cursor.getString(4)
val allDay = "All Day: " + cursor.getString(5)
val location = "Location: " + cursor.getString(6)
val organizer = "Organizer: " + cursor.getString(7)
val timeZone = "Timezone: " + cursor.getString(8)
val accountType = "Account Type: " + cursor.getString(9)
val accountName = "Account Name: " + cursor.getString(10)
calendarString =
calendarId + "\n" + title + "\n" + description + "\n" + startDate + "\n" + endDate + "\n" + allDay + "\n" + location + "\n" + organizer + "\n" + timeZone + "\n" + accountType + "\n" + accountName
result = if (result.isEmpty()) {
calendarString
} else {
result + "\n\n\n" + calendarString
}
count++
} while (cursor.moveToNext())
}
// txtResults.text = result
Log.e("DCal", "" + count)
getEventsReminders(selectedEmail)
}
这绝对完美,完美。但是,当我尝试使用以下代码从设备中获取提醒时,
@SuppressLint("MissingPermission")
private fun getAccountCalendarReminders(selectedEmail: String) {
var count = 0
val projectionArray =
arrayOf(
CalendarContract.Reminders.EVENT_ID,
CalendarContract.Reminders.METHOD,
CalendarContract.Reminders.MINUTES
)
val startTime = Calendar.getInstance()
startTime.add(Calendar.YEAR, -2)
val endTime = Calendar.getInstance()
endTime.add(Calendar.YEAR, 2)
// val selection =
// "((account_name = '$selectedEmail'))"
val cursor = contentResolver.query(
CalendarContract.Reminders.CONTENT_URI,
projectionArray,
null,
null,
null
)
if (cursor != null && cursor.count > 0 && cursor.moveToFirst()) {
do {
var calendarString = ""
val calendarId = "Id: " + cursor.getInt(0)
val title = "Method: " + cursor.getString(1)
val description = "Minutes: " + cursor.getString(2)
// val startDate = "Title: " + cursor.getString(3)
// val endDate = "End Date: " + cursor.getString(4)
// val allDay = "All Day: " + cursor.getString(5)
// val location = "Location: " + cursor.getString(6)
// val organizer = "Organizer: " + cursor.getString(7)
// val timeZone = "Timezone: " + cursor.getString(8)
// val accountType = "Account Type: " + cursor.getString(9)
// val accountName = "Account Name: " + cursor.getString(10)
calendarString =
calendarId + "\n" + title + "\n" + description
result = if (result.isEmpty()) {
calendarString
} else {
result + "\n\n\n" + calendarString
}
count++
} while (cursor.moveToNext())
}
Log.e("DRem", "" + count)
txtResults.text = result
}
提醒的输出为
任何人都可以帮助我获得完全一样的提醒信息, 1.提醒名称, 2.开始日期, 3.位置等,事件具有的相同字段。
注意: 我也浏览了这个库,
https://github.com/EverythingMe/easy-content-providers
此库执行相同的操作。它不会从设备日历中列出提醒。
上述库中的代码段:
val provider = CalendarProvider(this)
val events = provider.getEvents(id.toLong()).list as ArrayList<Event>
val reminders = provider.getReminders(id.toLong()).list as ArrayList<Event>