简体   繁体   中英

Android: Unable to add event on calendar in andorid

Android: I am trying to add an event on the android calendar without opening android calendar app(Implicit Intent). Getting eventId successfully after calling below funcation onAddEventClicked(), but event is not getting saved and not showing in the calendar app.

Anybody please help me. Thanks in advance.

 AndroidMainfest.xml
    <uses-permission android:name="android.permission.READ_CALENDAR"/>
    <uses-permission android:name="android.permission.WRITE_CALENDAR"/>  



 fun onAddEventClicked(){
        val calID: Long = 3
        val startMillis: Long = Calendar.getInstance().run {
            set(2019, 10, 3, 7, 30)
            timeInMillis
        }
        val endMillis: Long = Calendar.getInstance().run {
            set(2019, 10, 5, 8, 45)
            timeInMillis
        }


        val values = ContentValues().apply {
            put(CalendarContract.Events.DTSTART, startMillis)
            put(CalendarContract.Events.DTEND, endMillis)
            put(CalendarContract.Events.TITLE, "My birthday")
            put(CalendarContract.Events.DESCRIPTION, "Celebration day")
            put(CalendarContract.Events.CALENDAR_ID, calID)
            put(CalendarContract.Events.EVENT_TIMEZONE,  TimeZone.getDefault().id)
        }
        val uri: Uri = contentResolver.insert(CalendarContract.Events.CONTENT_URI, values)

        val eventID: Long = uri.lastPathSegment.toLong()
    }

This is what i came up with to add an event to calendar in my application:

    Calendar calAppointment = Calendar.getInstance();
    calAppointment.setTime(appointment.getAppointmentDate());
    calAppointment.set(Calendar.HOUR_OF_DAY, 12);
    calAppointment.set(Calendar.MINUTE, 30);
    Intent intent = new Intent(Intent.ACTION_INSERT);
    intent.setType("vnd.android.cursor.item/event");
    intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, calAppointment.getTime().getTime());
    intent.putExtra(CalendarContract.Events.EVENT_TIMEZONE, "Your Timezone");
    intent.putExtra(CalendarContract.Events.TITLE, "Title");
    intent.putExtra(CalendarContract.Events.DESCRIPTION, "Your description");
    intent.putExtra(CalendarContract.Events.EVENT_LOCATION, "Your location");
    if (intent.resolveActivityInfo(getActivity().getPackageManager(), 0) != null) {
        startActivity(intent);
    }

Hope this can help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM