Last active
April 4, 2021 04:01
-
-
Save ThePyProgrammer/cf857009dd16e68dc92f90e06bb490f5 to your computer and use it in GitHub Desktop.
Implementing Calendar intent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Begin code with LocalDate object date and LocalTime objects begin and end, and String objects title, content, location | |
Boolean allDay = begin.equals(LocalTime.of(0, 0)) && end.equals(LocalTime.of(23, 59)) | |
LocalDateTime startTime = date.atTime(begin) | |
LocalDateTime endTime = date.atTime(end) | |
Calendar startCal = Calendar.getInstance() | |
startCal.set( | |
startTime.year, | |
startTime.monthValue - 1, | |
startTime.dayOfMonth, | |
startTime.hour, | |
startTime.minute | |
) | |
long startMillis = startCal.timeInMillis | |
Calendar endCal = Calendar.getInstance() | |
endCal.set( | |
endTime.year, | |
endTime.monthValue - 1, | |
endTime.dayOfMonth, | |
endTime.hour, | |
endTime.minute | |
) | |
long endMillis = endCal.timeInMillis | |
Intent calIntent = Intent(Intent.ACTION_INSERT) | |
calIntent.setType("vnd.android.cursor.item/event") | |
calIntent.setData(Events.CONTENT_URI) | |
calIntent.putExtra(Events.TITLE, title) // Usage for title | |
calIntent.putExtra(Events.ALL_DAY, allDay) | |
calIntent.putExtra(Events.DESCRIPTION, content) // Usage for content | |
calIntent.putExtra( | |
CalendarContract.EXTRA_EVENT_BEGIN_TIME, | |
startMillis | |
) | |
calIntent.putExtra( | |
CalendarContract.EXTRA_EVENT_END_TIME, | |
endMillis | |
) | |
calIntent.putExtra(Events.EVENT_LOCATION, location) // Usage for location | |
startActivity(calIntent) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Begin code with LocalDate object date and LocalTime objects begin and end, and String objects title, content, location | |
val allDay = begin == LocalTime.of(0, 0) && end == LocalTime.of(23, 59) | |
val startTime = date.atTime(begin) | |
val endTime = date.atTime(end) | |
val startMillis: Long = Calendar.getInstance().run { | |
set( | |
startTime.year, | |
startTime.monthValue - 1, | |
startTime.dayOfMonth, | |
startTime.hour, | |
startTime.minute | |
) | |
timeInMillis | |
} | |
val endMillis: Long = Calendar.getInstance().run { | |
set( | |
endTime.year, | |
endTime.monthValue - 1, | |
endTime.dayOfMonth, | |
endTime.hour, | |
endTime.minute | |
) | |
timeInMillis | |
} | |
val calIntent = Intent(Intent.ACTION_INSERT).apply { | |
type = "vnd.android.cursor.item/event" | |
data = Events.CONTENT_URI | |
putExtra(Events.TITLE, title) // Usage for title | |
putExtra(Events.ALL_DAY, allDay) | |
putExtra(Events.DESCRIPTION, content) // Usage for content | |
putExtra( | |
CalendarContract.EXTRA_EVENT_BEGIN_TIME, | |
startMillis | |
) | |
putExtra( | |
CalendarContract.EXTRA_EVENT_END_TIME, | |
endMillis | |
) | |
putExtra(Events.EVENT_LOCATION, location) // Usage for location | |
} | |
startActivity(calIntent) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment