How to share a calendar event that doesn't exists with share intent in Android?

26 Views Asked by At

Context: I have a QR scanner app that can scan calendar event. upon scanned, the user can add the calendar event or share it. how can I share (if I can) to another app?

That how I create the event adding intent:

fun Context.scheduleEvent(
    title: String,
    startDateMillis: Long,
    endDateMillis: Long,
    description: String,
    organizer: String,
    status: String,
    location: String
){
    try {
        val intent = Intent(Intent.ACTION_EDIT)
        intent. Type = "vnd.android.cursor.item/event"
        intent.putExtra(CalendarContract.Events.TITLE, title)
        intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, startDateMillis)
        intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endDateMillis)
        intent.putExtra(CalendarContract.Events.ALL_DAY, false)// periodicity
        intent.putExtra(CalendarContract.Events.DESCRIPTION, description)
        intent.putExtra(CalendarContract.Events.ORGANIZER, organizer)
        intent.putExtra(CalendarContract.Events.EVENT_LOCATION, location)
        intent.putExtra(CalendarContract.Events.STATUS, status)
        startActivity(intent)
    } catch (e: ActivityNotFoundException) {
        Toast.makeText(this, this.getString(R.string.no_app), Toast.LENGTH_SHORT).show()
    } catch (t: Throwable) {
        t.printStackTrace()
    }
}
1

There are 1 best solutions below

1
Vijay On

To share the scanned calendar event to another app, you'll want to use an Intent with the ACTION_SEND action. This is a generic way to share data between apps. When you use this intent, a chooser dialog will pop up and show all the apps that can accept the shared data.

Here's how you can implement a sharing feature:

fun Context.shareEventDetails(
    title: String,
    startDate: String,
    endDate: String,
    description: String,
    organizer: String,
    location: String
) {
    try {
        val shareText = """
            Event Details:
            Title: $title
            Start Date: $startDate
            End Date: $endDate
            Description: $description
            Organizer: $organizer
            Location: $location
        """.trimIndent()

        val shareIntent = Intent(Intent.ACTION_SEND).apply {
            type = "text/plain"
            putExtra(Intent.EXTRA_SUBJECT, "Event Details")
            putExtra(Intent.EXTRA_TEXT, shareText)
        }
        startActivity(Intent.createChooser(shareIntent, "Share event details via"))
    } catch (t: Throwable) {
        t.printStackTrace()
    }
}

To call the shareEventDetails method, you'd just pass the necessary parameters:

shareEventDetails(
    title = "Sample Event",
    startDate = "2023-10-07 10:00:00",
    endDate = "2023-10-07 12:00:00",
    description = "This is a sample event.",
    organizer = "[email protected]",
    location = "123 Event St, City, Country"
)

This will allow the user to share the event details to any app that can accept plain text, like messaging apps, email clients, note-taking apps, etc.