. f Android add calendar event Programatically | Android Codes: Insertion and Deletion of Calendar Events | Configure email notification with Calendar app in Android ~ Android Developers Blog

Monday, 6 July 2015

Android add calendar event Programatically | Android Codes: Insertion and Deletion of Calendar Events | Configure email notification with Calendar app in Android

Hello Friends,
          Today, I am sharing my another android tutorial which covers following things:

1. Android- Adding events to calendar via your android application
2. Programmatically add reminder in android calendara
3. Configure email notification with Calendar app in Android


A. For adding event on Calendar

  1. try {  
  2.  String eventUriString = "content://com.android.calendar/events";  
  3.  ContentValues eventValues = new ContentValues();  
  4.  eventValues.put("calendar_id"1); // id, We need to choose from  
  5.      // our mobile for primary its 1  
  6.  eventValues.put("title", title);  
  7.  eventValues.put("description", desc);  
  8.  eventValues.put("eventLocation", place);  
  9.   
  10.  long endDate = startDate + 1000 * 10 * 10// For next 10min  
  11.  eventValues.put("dtstart", startDate);  
  12.  eventValues.put("dtend", endDate);  
  13.   
  14.  // values.put("allDay", 1); //If it is bithday alarm or such  
  15.  // kind (which should remind me for whole day) 0 for false, 1  
  16.  // for true  
  17.   eventValues.put("eventStatus", status); // This information is  
  18.    // sufficient for most  
  19.    // entries tentative (0),  
  20.    // confirmed (1) or canceled  
  21.    // (2):  
  22.  eventValues.put("eventTimezone""UTC/GMT +5:30");  
  23.  /* 
  24.   * Comment below visibility and transparency column to avoid 
  25.   * java.lang.IllegalArgumentException column visibility is invalid 
  26.   * error 
  27.   */  
  28.  // eventValues.put("allDay", 1);  
  29.  // eventValues.put("visibility", 0); // visibility to default (0),  
  30.    // confidential (1), private  
  31.    // (2), or public (3):  
  32.    // eventValues.put("transparency", 0); // You can control whether  
  33.    // an event consumes time  
  34.    // opaque (0) or transparent  
  35.    // (1).  
  36.  eventValues.put("hasAlarm"1); // 0 for false, 1 for true  
  37.   
  38.  Uri eventUri = curActivity.getApplicationContext()  
  39.     .getContentResolver()  
  40.     .insert(Uri.parse(eventUriString), eventValues);  
  41.  eventID = Long.parseLong(eventUri.getLastPathSegment());   
  42. catch (Exception ex) {  
  43.  log.error("Error in adding event on calendar" + ex.getMessage());  
  44. }  


B. Set Reminder for above added event id:

  1. if (needReminder) {  
  2. /***************** Event: Reminder(with alert) Adding reminder to event *******************/  
  3.         String reminderUriString = "content://com.android.calendar/reminders";  
  4.  ContentValues reminderValues = new ContentValues();  
  5.  reminderValues.put("event_id", eventID);  
  6.  reminderValues.put("minutes"5); // Default value   
  7.        //set time in min which occur before event start     
  8.  reminderValues.put("method"1); // Alert Methods: Default(0),  
  9.      // Alert(1), Email(2),SMS(3)  
  10.  Uri reminderUri = curActivity.getApplicationContext()  
  11.       .getContentResolver()  
  12.     .insert(Uri.parse(reminderUriString), reminderValues);  
  13. }  
  14.    

C: Send mail to user for attending the meeting added on calendar

  1. /***************** Event: Meeting(without alert) Adding Attendies to the meeting *******************/  
  2.   
  3. if (needMailService) {  
  4.  String attendeuesesUriString = "content://com.android.calendar/attendees";  
  5.   
  6.  /******** 
  7.   * To add multiple attendees need to insert ContentValues 
  8.   * multiple times 
  9.   ***********/  
  10.  ContentValues attendeesValues = new ContentValues();  
  11.  attendeesValues.put("event_id", eventID);  
  12.  attendeesValues.put("attendeeName""xxxxx"); // Attendees name  
  13.  attendeesValues.put("attendeeEmail""yyyy@gmail.com");// Attendee email  
  14.  attendeesValues.put("attendeeRelationship"0); // Relationship_Attendee(1),  
  15.        // Relationship_None(0),  
  16.        // Organizer(2),  
  17.        // Performer(3),  
  18.        // Speaker(4)  
  19.  attendeesValues.put("attendeeType"0); // None(0), Optional(1),  
  20.       // Required(2),  
  21.       // Resource(3)  
  22.  attendeesValues.put("attendeeStatus"0); // NOne(0),  
  23.           // Accepted(1),  
  24.         // Decline(2),  
  25.         // Invited(3),  
  26.         // Tentative(4)  
  27.   
  28.  Uri eventsUri = Uri.parse("content://calendar/events");  
  29.  Uri url = curActivity.getApplicationContext()  
  30.     .getContentResolver()  
  31.     .insert(eventsUri, attendeesValues);  
  32.   
  33.  // Uri attendeuesesUri = curActivity.getApplicationContext()  
  34.     // .getContentResolver()  
  35.     // .insert(Uri.parse(attendeuesesUriString), attendeesValues);  
  36. }   

D: For deletion of calendar event: Here "id" is event id.

  1. Uri uri = ContentUris.withAppendedId(CALENDAR_URI, Integer.parseInt(id));  
  2. getContentResolver().delete(uri, nullnull);   

Finally All together: Add this method in your activity class

  1. public long addAppointmentsToCalender(Activity curActivity, String title,  
  2.    String desc, String place, int status, long startDate,  
  3.    boolean needReminder, boolean needMailService) {  
  4. /***************** Event: add event *******************/  
  5. long eventID = -1;  
  6. try {  
  7.         String eventUriString = "content://com.android.calendar/events";  
  8.  ContentValues eventValues = new ContentValues();  
  9.  eventValues.put("calendar_id"1); // id, We need to choose from  
  10.               // our mobile for primary its 1  
  11.  eventValues.put("title", title);  
  12.  eventValues.put("description", desc);  
  13.  eventValues.put("eventLocation", place);  
  14.   
  15.  long endDate = startDate + 1000 * 10 * 10// For next 10min  
  16.  eventValues.put("dtstart", startDate);  
  17.  eventValues.put("dtend", endDate);  
  18.   
  19.  // values.put("allDay", 1); //If it is bithday alarm or such  
  20.    // kind (which should remind me for whole day) 0 for false, 1  
  21.    // for true  
  22.  eventValues.put("eventStatus", status); // This information is  
  23.    // sufficient for most  
  24.    // entries tentative (0),  
  25.    // confirmed (1) or canceled  
  26.    // (2):  
  27.  eventValues.put("eventTimezone""UTC/GMT +5:30");  
  28.  /* 
  29.   * Comment below visibility and transparency column to avoid 
  30.   * java.lang.IllegalArgumentException column visibility is invalid 
  31.   * error 
  32.   */  
  33.  // eventValues.put("allDay", 1);  
  34.  // eventValues.put("visibility", 0); // visibility to default (0),  
  35.                                       // confidential (1), private  
  36.                                       // (2), or public (3):  
  37.  // eventValues.put("transparency", 0); // You can control whether  
  38.                                         // an event consumes time  
  39.                           // opaque (0) or transparent (1).  
  40.      
  41.         eventValues.put("hasAlarm"1); // 0 for false, 1 for true  
  42.   
  43.  Uri eventUri = curActivity.getApplicationContext()  
  44.     .getContentResolver()  
  45.     .insert(Uri.parse(eventUriString), eventValues);  
  46.  eventID = Long.parseLong(eventUri.getLastPathSegment());  
  47.   
  48.  if (needReminder) {  
  49.  /***************** Event: Reminder(with alert) Adding reminder to event ***********        ********/  
  50.    
  51.         String reminderUriString = "content://com.android.calendar/reminders";  
  52.         ContentValues reminderValues = new ContentValues();  
  53.  reminderValues.put("event_id", eventID);  
  54.  reminderValues.put("minutes"5); // Default value of the  
  55.        // system. Minutes is a integer  
  56.  reminderValues.put("method"1); // Alert Methods: Default(0),  
  57.      // Alert(1), Email(2),SMS(3)  
  58.   
  59.  Uri reminderUri = curActivity.getApplicationContext()  
  60.      .getContentResolver()  
  61.      .insert(Uri.parse(reminderUriString), reminderValues);  
  62.  }  
  63.   
  64. /***************** Event: Meeting(without alert) Adding Attendies to the meeting *******************/  
  65.   
  66.  if (needMailService) {  
  67.  String attendeuesesUriString = "content://com.android.calendar/attendees";  
  68.  /******** 
  69.   * To add multiple attendees need to insert ContentValues 
  70.   * multiple times 
  71.   ***********/  
  72.  ContentValues attendeesValues = new ContentValues();  
  73.  attendeesValues.put("event_id", eventID);  
  74.  attendeesValues.put("attendeeName""xxxxx"); // Attendees name  
  75.  attendeesValues.put("attendeeEmail""yyyy@gmail.com");// Attendee Email  
  76.  attendeesValues.put("attendeeRelationship"0); // Relationship_Attendee(1),  
  77.        // Relationship_None(0),  
  78.        // Organizer(2),  
  79.        // Performer(3),  
  80.        // Speaker(4)  
  81.  attendeesValues.put("attendeeType"0); // None(0), Optional(1),  
  82.       // Required(2),  
  83.       // Resource(3)  
  84.  attendeesValues.put("attendeeStatus"0); // NOne(0),  
  85.         // Accepted(1),  
  86.         // Decline(2),  
  87.         // Invited(3),  
  88.         // Tentative(4)  
  89.   
  90.  Uri eventsUri = Uri.parse("content://calendar/events");  
  91.  Uri url = curActivity.getApplicationContext()  
  92.     .getContentResolver()  
  93.     .insert(eventsUri, attendeesValues);  
  94.   
  95.  // Uri attendeuesesUri = curActivity.getApplicationContext()  
  96.     // .getContentResolver()  
  97.     // .insert(Uri.parse(attendeuesesUriString), attendeesValues);  
  98.  }  
  99. catch (Exception ex) {  
  100.  log.error("Error in adding event on calendar" + ex.getMessage());  
  101. }  
  102.   
  103. return eventID;  
  104.   
  105. }  
  106.    

Also need to add following permission in your AndroidManifest.xml file

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


Hope this will help some one.

Enjoy Coding :)

Mukesh Kumar

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....

11 comments:

  1. thanks for the guide but if possible, can you upload the full code of them including the layout?

    ReplyDelete
  2. Can i get entire code plz?plz send to reena@maksinfotech.com

    ReplyDelete
  3. Hello,
    I have provided the call which you just need to bind with your ui. If you still
    face the issue then drop me your sample code and I will check and fix it.
    My id is : himky02@gmail.com.

    ReplyDelete
  4. Hi Mukesh,

    I am trying to build a custom calendar for events.
    Read,delete,update and show specific event list.

    Till now my code is working fine to read calendar events and display the same. but i am not able to integrate your given code to add event ....
    Can you please help me...

    I am using a Tab layout in my app. One tab is EVENT tab Onclick of event tab I am displaying event calendar and on button click trying to Add events....

    TO read and display calendar events I referred below link
    http://www.androiddevelopersolutions.com/2013/05/android-calendar-sync.html

    ReplyDelete
  5. Hi Mukesh,

    I am trying to build a custom calendar for events.
    Read,delete,update and show specific event list.

    Till now my code is working fine to read calendar events and display the same. but i am not able to integrate your given code to add event ....
    Can you please help me...

    I am using a Tab layout in my app. One tab is EVENT tab Onclick of event tab I am displaying event calendar and on button click trying to Add events....

    TO read and display calendar events I referred below link
    http://www.androiddevelopersolutions.com/2013/05/android-calendar-sync.html

    ReplyDelete
  6. Hi Mukesh,

    I am trying to build a custom calendar to display events.
    Functions - Add event,Delete,remainders,update,display event event..

    Till now my code working fine for custom calendar and display calendar events but i am not able to integrate your above code whenever i am trying to integrate my app stopped unfortunately.


    In my project i am using tab layout...one of my tab is Event tab and using fragments I am trying to display custom calendar .. i need to add event on button click..


    Can you please help me .....

    I referred the custom calendar and display event code from below link :
    http://www.androiddevelopersolutions.com/2013/05/android-calendar-sync.html

    ReplyDelete
  7. Hi mukesh, i am getting this error when i click date which has an event:
    java.lang.IndexOutOfBoundsException: Invalid index 46, size is 12

    ReplyDelete
  8. Im having trouble implementing your codes on my code

    ReplyDelete
  9. Can i get entire code.
    plz send it to srinivasannamachivayan@gmail.com

    ReplyDelete
  10. how can i get a current time event in my application

    ReplyDelete
  11. hi how can a show notification from my custom calendar when particular events occur

    ex: if i added event for tomarrow at 1pm it my app should throw notification at tmrw 1pm plz help me

    ReplyDelete

 

Copyright @ 2013 Android Developers Blog.