36

I am trying to build a calendar app for Android. I am struck in the middle. I have managed to retrieve the information such as time and task from the user.

I don't know how to add this into android events. Is there anything like setEvent or something similar?

user2428118
  • 7,935
  • 4
  • 45
  • 72
Andro Selva
  • 53,910
  • 52
  • 193
  • 240

3 Answers3

81

Nope, it is more complicated than just calling a method, if you want to transparently add it into the user's calendar.

You've got a couple of choices;

  1. Calling the intent to add an event on the calendar
    This will pop up the Calendar application and let the user add the event. You can pass some parameters to prepopulate fields:

    Calendar cal = Calendar.getInstance();              
    Intent intent = new Intent(Intent.ACTION_EDIT);
    intent.setType("vnd.android.cursor.item/event");
    intent.putExtra("beginTime", cal.getTimeInMillis());
    intent.putExtra("allDay", false);
    intent.putExtra("rrule", "FREQ=DAILY");
    intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
    intent.putExtra("title", "A Test Event from android app");
    startActivity(intent);
    

    Or the more complicated one:

  2. Get a reference to the calendar with this method
    (It is highly recommended not to use this method, because it could break on newer Android versions):

    private String getCalendarUriBase(Activity act) {
    
        String calendarUriBase = null;
        Uri calendars = Uri.parse("content://calendar/calendars");
        Cursor managedCursor = null;
        try {
            managedCursor = act.managedQuery(calendars, null, null, null, null);
        } catch (Exception e) {
        }
        if (managedCursor != null) {
            calendarUriBase = "content://calendar/";
        } else {
            calendars = Uri.parse("content://com.android.calendar/calendars");
            try {
                managedCursor = act.managedQuery(calendars, null, null, null, null);
            } catch (Exception e) {
            }
            if (managedCursor != null) {
                calendarUriBase = "content://com.android.calendar/";
            }
        }
        return calendarUriBase;
    }
    

    and add an event and a reminder this way:

    // get calendar
    Calendar cal = Calendar.getInstance();     
    Uri EVENTS_URI = Uri.parse(getCalendarUriBase(this) + "events");
    ContentResolver cr = getContentResolver();
    
    // event insert
    ContentValues values = new ContentValues();
    values.put("calendar_id", 1);
    values.put("title", "Reminder Title");
    values.put("allDay", 0);
    values.put("dtstart", cal.getTimeInMillis() + 11*60*1000); // event starts at 11 minutes from now
    values.put("dtend", cal.getTimeInMillis()+60*60*1000); // ends 60 minutes from now
    values.put("description", "Reminder description");
    values.put("visibility", 0);
    values.put("hasAlarm", 1);
    Uri event = cr.insert(EVENTS_URI, values);
    
    // reminder insert
    Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(this) + "reminders");
    values = new ContentValues();
    values.put( "event_id", Long.parseLong(event.getLastPathSegment()));
    values.put( "method", 1 );
    values.put( "minutes", 10 );
    cr.insert( REMINDERS_URI, values );
    

    You'll also need to add these permissions to your manifest for this method:

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

Update: ICS Issues

The above examples use the undocumented Calendar APIs, new public Calendar APIs have been released for ICS, so for this reason, to target new android versions you should use CalendarContract.

More infos about this can be found at this blog post.

BFil
  • 12,966
  • 3
  • 44
  • 48
  • `events.add(event);` Can you plz tell me, what you are referring to here. because I dont know what "events" means and It shows error. – Andro Selva May 12 '11 at 13:36
  • sorry, I was referring to a global variable in my code to keep all the events in a list, it isn't needed =) i'll edit the answer – BFil May 12 '11 at 13:39
  • And what should i provide instead of "act". – Andro Selva May 12 '11 at 14:11
  • another wrong reference, damn, i'll update the answer, getCalendarUriBase(Activity act) expects an activity as argument, if you are inside your activity, just provide "this" ;) – BFil May 12 '11 at 14:15
  • 1
    05-12 19:46:20.704: ERROR/AndroidRuntime(368): Caused by: java.lang.IllegalArgumentException: Unknown URL nullevents 05-12 19:46:20.704: ERROR/AndroidRuntime(368): at android.content.ContentResolver.insert(ContentResolver.java:626) But still I get these errors when I try run this in my device – Andro Selva May 12 '11 at 14:21
  • getCalendarUriBase is returning you null instead of the uri of the calendar content provider, that is what it returns if it doesn't find any valid content provider, the emulator doesn't have a calendar content provider – BFil May 12 '11 at 14:33
  • I tried it in my mobile which has its calendar. The above intent method works, but this one ends with error. – Andro Selva May 12 '11 at 14:36
  • mmm.. so maybe the device have a different content provider than the standard ones.. I tested it on Nexus One, Galaxy Tab and LG Optimus One, which device are you using? That is why is reccomented not to be done this way I think =D it worked on all of my devices, maybe I've been just lucky.. – BFil May 12 '11 at 14:46
  • Hey, I am displaing my Event lists (which are in calendar) in listView and on that listview click I used your code "Calling the intent to add an event on the calendar". Thats ok its work perfectly. But when we click on Back button it will generate new Event. So I want to handle that, I want to disable that back button for that screen,OR I want to perform edit functionality instead of Create New Event. And also I want to remove notification when Event Create. Can we perform these operation? Please help me I am stuck on this. – anddev Aug 05 '11 at 11:34
  • What actually these two statement do..values.put( "event_id", Long.parseLong(event.getLastPathSegment())); values.put( "method", 1 ); – AndroidDev Sep 15 '11 at 06:20
  • @user2428228 Hie... I'ma newbie..., Can you please explain this code a litle bit, So that I can implement this in my code – Sumit Sharma Dec 10 '12 at 07:51
  • Hello all i want to use calendar but i never used before and i am facing some issue like i want to add event to calendar in device.. i want to add event directly from background and not by opening activity of calendar is any way for this? i tried using both the codes but first one is not useful for me and second one is giving many errors so can anybody tell me the solution? – Mahaveer Muttha Jan 11 '13 at 13:23
  • i got error on this line: Uri event = cr.insert(EVENTS_URI, values); and error: event value must include an eventTimezove – Gold Apr 22 '13 at 18:45
  • Check this: http://stackoverflow.com/questions/9533757/crash-occur-while-running-app-in-ice-cream-sandwich – BFil Apr 25 '13 at 09:40
  • is there any method to get Event_id using 1st method?? i.e , **1. Calling the intent to add an event on the calendar** – Aayushi May 14 '14 at 13:09
  • Hello, using the number 1 solution, how do I set a notification 1 hour before the event start? – Simone Sep 18 '16 at 12:35
21

You can use AlarmManager in coop with notification mechanism Something like this:

Intent intent = new Intent(ctx, ReminderBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) ctx.getSystemService(Activity.ALARM_SERVICE);
// time of of next reminder. Unix time.
long timeMs =...
if (Build.VERSION.SDK_INT < 19) {
    am.set(AlarmManager.RTC_WAKEUP, timeMs, pendingIntent);
} else {
    am.setExact(AlarmManager.RTC_WAKEUP, timeMs, pendingIntent);
}

It starts alarm.

public class ReminderBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setSmallIcon(...)
                .setContentTitle(..)
                .setContentText(..);
        Intent intentToFire = new Intent(context, Activity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intentToFire, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(pendingIntent);
        NotificationManagerCompat.from(this);.notify((int) System.currentTimeMillis(), builder.build());
    }
}
Divers
  • 9,531
  • 7
  • 45
  • 88
12

Android complete source code for adding events and reminders with start and end time format.

/** Adds Events and Reminders in Calendar. */
private void addReminderInCalendar() {
    Calendar cal = Calendar.getInstance();
    Uri EVENTS_URI = Uri.parse(getCalendarUriBase(true) + "events");
    ContentResolver cr = getContentResolver();
    TimeZone timeZone = TimeZone.getDefault();

    /** Inserting an event in calendar. */
    ContentValues values = new ContentValues();
    values.put(CalendarContract.Events.CALENDAR_ID, 1);
    values.put(CalendarContract.Events.TITLE, "Sanjeev Reminder 01");
    values.put(CalendarContract.Events.DESCRIPTION, "A test Reminder.");
    values.put(CalendarContract.Events.ALL_DAY, 0);
    // event starts at 11 minutes from now
    values.put(CalendarContract.Events.DTSTART, cal.getTimeInMillis() + 11 * 60 * 1000);
    // ends 60 minutes from now
    values.put(CalendarContract.Events.DTEND, cal.getTimeInMillis() + 60 * 60 * 1000);
    values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
    values.put(CalendarContract.Events.HAS_ALARM, 1);
    Uri event = cr.insert(EVENTS_URI, values);

    // Display event id
    Toast.makeText(getApplicationContext(), "Event added :: ID :: " + event.getLastPathSegment(), Toast.LENGTH_SHORT).show();

    /** Adding reminder for event added. */
    Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(true) + "reminders");
    values = new ContentValues();
    values.put(CalendarContract.Reminders.EVENT_ID, Long.parseLong(event.getLastPathSegment()));
    values.put(CalendarContract.Reminders.METHOD, Reminders.METHOD_ALERT);
    values.put(CalendarContract.Reminders.MINUTES, 10);
    cr.insert(REMINDERS_URI, values);
}

/** Returns Calendar Base URI, supports both new and old OS. */
private String getCalendarUriBase(boolean eventUri) {
    Uri calendarURI = null;
    try {
        if (android.os.Build.VERSION.SDK_INT <= 7) {
            calendarURI = (eventUri) ? Uri.parse("content://calendar/") : Uri.parse("content://calendar/calendars");
        } else {
            calendarURI = (eventUri) ? Uri.parse("content://com.android.calendar/") : Uri
                    .parse("content://com.android.calendar/calendars");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return calendarURI.toString();
}

Add permission to your Manifest file.

<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
Sanjeev Kumar
  • 193
  • 1
  • 7
  • @Sanjeev Kumar how we can add multiple times on One Event. I mean i want to add event before 1 hour , 20 minutes and 1 minute. – Umair Iqbal Oct 24 '17 at 11:38
  • @ Umair Iqbal :: For this you simply add the event in calendar as a new event all the event details will be same only time will change. Each event has its own id so there won't be any issue and it will work fine. – Sanjeev Kumar Oct 25 '17 at 15:13
  • @ pblead26 :: Just create a new project and use this method, providing required permissions. It will work. – Sanjeev Kumar Oct 25 '17 at 15:15