Alright, I worked on this sometime back and I will share this with you. I was looking to programmatically add events to the native Android calendar from a Phonegap android app. There is not an official plugin that is available as of now. So I wrote a custom workaround for this. Again, I am not a Java developer and there might be better ways of writing the plugin code. But just wanted to share the code, and any feedback would just be appreciated.
Check the two files out. I have inline comments that will help you to understand. Rest, is very simple to grasp. Now let’s check how to use and implement this plugin into your Phonegap Android project.
1) Firstly you have to add these two files to your Phonegap Android project. Add the CalendarEventPlugin.js file inside assets/www/ folder and provide a reference to it in your index.html file, like this
2) Then create a directory inside your project’s src folder that matches the package name of CalendarEventPlugin.java class. For our case make a directory – /org/apache/cordova/plugin inside src and then paste CalendarEventPlugin.java file inside it. If you change the package name, make sure to change the directory structure as well. The package name can be found at the top of CalendarEventPlugin.java file.
3) Next thing to do is to register the plugin in the config file – open res/xml/config.xml and then add the plugin details given below to the <plugins></plugins> section of the XML file. The name attribute is the Java class name and the value is the path of the class. This should match the package name.
<plugin name="CalendarEventPlugin" value="org.apache.cordova.plugin.CalendarEventPlugin"/>
alert(val); //once success message come and you have tested it, you can remove this alert.
Once everything runs fine, you will see that when you click a button or so in your demo app and it calls the callback function in step 4, the native Calendar Add event is popped up with values already set in it (which you can change in the java file of the plugin). From there on its just the normal way of adding events to Calendar.
Hope this helps!!
- I have tried this on an Android 4.0 device (Galaxy S3) and it works. I have not really tried this on lower Android versions.