karan
karan

Reputation: 8843

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?

Upvotes: 0

Views: 3540

Answers (2)

Naval Kishor Jha
Naval Kishor Jha

Reputation: 914

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

Upvotes: 2

Beena
Beena

Reputation: 2354

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);

Upvotes: 4

Related Questions