'Can I add event to calendar without letting user know in android?

I want to add event to my native calendar using the below code. and it is working fine.

SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm");
                final Calendar cal = Calendar.getInstance();
                try {
                    cal.setTime(formatter.parse(datetime));
                    eventdate = cal.get(Calendar.YEAR)+"/"+cal.get(Calendar.MONTH)+"/"+cal.get(Calendar.DAY_OF_MONTH)+" "+cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE);
                    Log.e("event date",eventdate);
                } 
                catch (ParseException e) {
                    e.printStackTrace();
                }
                Intent intent = new Intent(Intent.ACTION_EDIT);
                intent.setType("vnd.android.cursor.item/event");
                intent.putExtra("beginTime", cal.getTimeInMillis());
                intent.putExtra("allDay", true);
                intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
                intent.putExtra("title", title);
                intent.putExtra("description",desc);
                c.startActivity(intent);

This opens up a screen where it asks me to save event. and when I click on save, it saves my event to calendar.

Now, what I want is to add event directly to the native calendar, without showing the above screen. Is there any way to do that?



Solution 1:[1]

Intent intent = new Intent(Intent.ACTION_EDIT);
                intent.setType("vnd.android.cursor.item/event");
                intent.putExtra("beginTime", cal.getTimeInMillis());
                intent.putExtra("allDay", true);
                intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
                intent.putExtra("title", title);
                intent.putExtra("description",desc);
                c.startActivity(intent);

Don't use this because here by calling this c.startActivity(intent); you are starting an activity which will so the user in the calendar and ask to save it instead of using this you can use ContentResolver to insert the event with your information

and like this

            try
            {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm");
Calendar cal = Calendar.getInstance();
    cal.setTime(formatter.parse(datetime));
                        eventdate = cal.get(Calendar.YEAR)+"/"+cal.get(Calendar.MONTH)+"/"+cal.get(Calendar.DAY_OF_MONTH)+" "+cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE);
                        Log.e("event date",eventdate);
    // provide CalendarContract.Calendars.CONTENT_URI to
    // ContentResolver to query calendars
            Cursor      cur = this.getContentResolver().query(CalendarContract.Calendars.CONTENT_URI,null, null, null, null);
                if (cur.moveToFirst())
                {
                    long calendarID=cur.getLong(cur.getColumnIndex(CalendarContract.Calendars._ID));
    ContentValues eventValues = new ContentValues();
    // provide the required fields for creating an event to
    // ContentValues instance and insert event using
    // ContentResolver
            eventValues.put (CalendarContract.Events.CALENDAR_ID, calendarID);
            eventValues.put(CalendarContract.Events.TITLE, "Event 1"+title);
            eventValues.put(CalendarContract.Events.DESCRIPTION," Calendar API"+desc);
eventValues.put(CalendarContract.Events.ALL_DAY,true);
            eventValues.put(CalendarContract.Events.DTSTART, (cal.getTimeInMillis()+60*60*1000));
            eventValues.put(CalendarContract.Events.DTEND, cal.getTimeInMillis()+60*60*1000);
            eventValues.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().toString());
            Uri eventUri = this.getContentResolver().insert(CalendarContract.Events.CONTENT_URI, eventValues);
            eventID = ContentUris.parseId(eventUri);
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            finally
            {
                if (cur != null)
                {
                    cur.close();
                }
            }

Write this code in your save event button and it will work the way you are looking

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Muhammad Farhan Arif