When I was developing an APP whose API is 7 on my Nexus S, there was no problem in creating a new event on my calendar.
I used that code to get the location of the calendars on my phone:
cursor = cr.query(Uri.parse("content://com.android.calendar/calendars"), new String[]{ "_id", "displayname" }, null, null, null);
The problem came when I updated my nexus S to Android ICS 4.0. Without changing any code I got a mistake. On logcat I could read:
Of course cursor is null. Maybe any change on the calendar database? So, I'd like to know how I can create new calendar events developing an API 7 application on an Android 4.0
Thanks ;)
In Android 4.0 the calendar is in the same Uri as in Android 2.3. So I attach my code in case other people had the same problem.
public void addToCalendar(Context ctx, final String title, final String comment, final long dtstart, final long dtend) {
final ContentResolver cr = ctx.getContentResolver();
Cursor cursor ;
cursor = cr.query(Uri.parse("content://com.android.calendar/calendars"), new String[]{ "_id","calendar_displayName" }, null, null, null);
/*if (Integer.parseInt(Build.VERSION.SDK) == 8 )
cursor = cr.query(Uri.parse("content://com.android.calendar/calendars"), new String[]{ "_id", "displayname" }, null, null, null);
*/
//Get all the calendar ids and name available in the phone
if ( cursor.moveToFirst() ) {
final String[] calNames = new String[cursor.getCount()];
final int[] calIds = new int[cursor.getCount()];
for (int i = 0; i < calNames.length; i++) {
calIds[i] = cursor.getInt(0);
calNames[i] = cursor.getString(1);
cursor.moveToNext();
}
//Creation of a new event in calendar whose position is 0 on the phone
ContentValues cv = new ContentValues();
cv.put("calendar_id", calIds[0]);
cv.put("title", title);
cv.put("dtstart", dtstart );
cv.put("dtend", dtend);
cv.put("eventTimezone","Spain");
cv.put("description", comment );
//Insertion on the events of the calendar
cr.insert(Uri.parse("content://com.android.calendar/events"), cv);
/*Uri newEvent ;
if (Integer.parseInt(Build.VERSION.SDK) == 8 )
newEvent = cr.insert(Uri.parse("content://com.android.calendar/events"), cv);
*/
finish();
}
cursor.close();
}
You have all the info in: http://developer.android.com/guide/topics/providers/calendar-provider.html as John said before.
Calendar Provider is new to Android 4.0, so you may want to review the documentation:
http://developer.android.com/guide/topics/providers/calendar-provider.html
For versions prior to Froyo, I know that calendar was located at content://calendar/calendars
, but it's since changed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With