PhoneGap/Cordova calendar integration (Android)

McGarnagle picture McGarnagle · Jun 3, 2012 · Viewed 8.4k times · Source

I'm building an Android app using PhoneGap (aka Cordova), and am having trouble getting a calendar integration to work. Disclaimer: I'm a noob to both Android and PhoneGap, please bear with me.

All I'm trying to do is add an event to the user's calendar. Following this tutorial, I've created a Plugin that attempts to launch a Calendar Intent. The code looks like this:

public class CalendarPlugin extends Plugin {
public static final String NATIVE_ACTION_STRING="addToCalendar"; 
public static final String SUCCESS_PARAMETER="success"; 

@Override
public PluginResult execute(String action, JSONArray data, String callbackId) {
    if (NATIVE_ACTION_STRING.equals(action)) { 
        Calendar beginTime = Calendar.getInstance();
        beginTime.set(2012, 6, 19, 7, 30);
        Calendar endTime = Calendar.getInstance();
        endTime.set(2012, 6, 19, 8, 30);

        Intent calIntent = new Intent((Context) this.ctx, CalendarPlugin.class)
            .setAction(Intent.ACTION_INSERT)
            .putExtra(Events.TITLE, "A new event")
            .putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true)
            .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis())
            .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis());

        this.ctx.startActivity(calIntent);
        return new PluginResult(PluginResult.Status.OK, "Smashing success");
    }

return new PluginResult(PluginResult.Status.ERROR, "Didn't work bro");
}
}

The Javascript code to invoke this plugin is standard:

var CalendarPlugin = { 
    callNativeFunction: function (success, fail, resultType) { 
        if (cordova) {
            return cordova.exec( success, fail, 
                "org.myorg.appname.CalendarPlugin", 
                "addToCalendar", [resultType]);
        }
        else {
            alert("Calendar function is not available here.");
        }
    } 
};

The Android code is getting invoked (confirmed using a breakpoint). But the result sent back to the Javascript code is an error:

Unable to find explicit activity class {org.myorg.appname/org.myorg.appname.CalendarPlugin}; have you declared this activity in your AndroidManifest.xml?

There's no mention of adding to AndroidManifest.xml in the tutorial, which leads me to believe I'm missing something (also, the CalendarPlugin code is being invoked successfully, so how could there be an error saying the CalendarPlugin class could not be found?). If indeed I need to add CalendarPlugin to the manifest, how would I go about doing that?

Answer

Morrison Chang picture Morrison Chang · Jun 4, 2012

The tutorial cited didn't cover intents. The intent you are sending your data to is your own CalendarPlugIn class which isn't what you want as it doesn't handle the intent.

See http://developer.android.com/guide/topics/intents/intents-filters.html regarding intents.

Also if you search around SO you'll find that until ICS there wasn't even a way to add things to the Google Calendar officially without using a web service. There are ways to do it unofficially, but are subject to the whims of Google or the ODMs themselves.

Updated:

You should be able to use intents with Phonegap (via a plugin). I only added the comment to note that if you intend to have calendar integration in your app, you'll probably have to do a bit of research if you want to support the majority of Android devices. If you are interested in adding calendar events in ICS look at: http://developer.android.com/reference/android/provider/CalendarContract.html

Op's Edit

I just needed to fix the Intent constructor, and this worked as expected:

Uri uri = Uri.parse("content://com.android.calendar/events");
Intent calIntent = new Intent("android.intent.action.INSERT", uri)