Pages

Thursday, 3 January 2013

Adding and removing events to/from calendar with content provider From android Application


private long pushAppointmentsToCalender(ContentResolver cr, String title, String addInfo, String place, long startDate, long endDate) {

Uri eventUriString = Uri.parse("content://com.android.calendar/events");

ContentValues eventValues = new ContentValues();

eventValues.put("calendar_id", 1); // id, We need to choose from our mobile for primary its 1
eventValues.put("title", title);
eventValues.put("description", addInfo);
eventValues.put("eventLocation", place);
eventValues.put("dtstart", startDate);
eventValues.put("dtend", endDate);
eventValues.put("eventTimezone", TimeZone.getDefault().toString());
eventValues.put("eventStatus", 1); // This information is sufficient for most entries tentative (0), confirmed (1) or canceled (2):
eventValues.put("hasAlarm", 0); // 0 for false, 1 for true

Uri eventUri = cr.insert(eventUriString, eventValues);
long eventID = Long.parseLong(eventUri.getLastPathSegment());
//System.out.println("event id is::"+eventID);
return eventID;

}


/** user defined method to get the next event id based on max_id+1 */
private  long getNewEventId(ContentResolver cr){    
   Uri local_uri = Uri.parse("content://com.android.calendar/events");
   Cursor cursor = cr.query(local_uri, new String [] {"MAX(_id) as max_id"}, null, null, "_id");
   cursor.moveToFirst();
   long max_val = cursor.getLong(cursor.getColumnIndex("max_id"));  
   return max_val+1;
}


/** user defined method to delete the event based on id*/
private int deleteEventFromCalendar(ContentResolver  cr,long id){
Uri eventUri = Uri.parse("content://com.android.calendar/events");  // or
Uri deleteUri = null;
deleteUri = ContentUris.withAppendedId(eventUri, id);
int rows = cr.delete(deleteUri, null, null);
// System.out.println("Rows deleted: " + rows);
return rows;
}


No comments:

Post a Comment