androidandroid-intentandroid-calendarandroid-event

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

  • You can refer below code to add events in calender.

    String GLOBAL_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    String gameDate="2015-07-12 01:10:00";
            Date startDate = DateConstants.getDateFromString(
                    gameDate,GLOBAL_DATE_FORMAT);
            long endDate = startDate.getTime()
                    + (PTGConstantMethod.validateInteger(game
                            .getGame_duration()) * 1000);
    
            ContentValues event = new ContentValues();
            event.put("calendar_id", 1);
            event.put("title", "Game#" + game.getId());
            event.put("description", game.getLocation());
            event.put("eventLocation", game.getLocation());
            event.put("eventTimezone", TimeZone.getDefault().getID());
            event.put("dtstart", startDate.getTime());
            event.put("dtend", endDate);
    
            event.put("allDay", 0); // 0 for false, 1 for true
            event.put("eventStatus", 1);
            event.put("hasAlarm", 1); // 0 for false, 1 for true
    
            String eventUriString = "content://com.android.calendar/events";
            Uri eventUri = context.getApplicationContext()
                    .getContentResolver()
                    .insert(Uri.parse(eventUriString), event);
            long eventID = Long.parseLong(eventUri.getLastPathSegment());
    
    // if reminder need to set
    
    
                int minutes=120;
    
    
                // add reminder for the event
                ContentValues reminders = new ContentValues();
                reminders.put("event_id", eventID);
                reminders.put("method", "1");
                reminders.put("minutes", minutes);
    
                String reminderUriString = "content://com.android.calendar/reminders";
                context.getApplicationContext().getContentResolver()
                .insert(Uri.parse(reminderUriString), reminders);