Insert, Delete events in Android Calendar...

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Insert, Delete events in Android Calendar...

Postby techkanika » Fri Oct 29, 2010 8:06 am

Insertion and Deletion of Calendar Events

As you know we have content providers for addition, deletion of Contacts, Image,Video and Phone Call Log etc.
Similarly we have Content Providers for Calendar Events..

This tutorial helps you in adding and deleting events in android 2.2 calendar. You need to follow the given steps :

1.) Getting required Calendar Name and Id :

A device may have more than one Calendar configure in it. So first, you must find
out available calendar's name and their id.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Cursor cursor=getContentResolver().query(Uri.parse("content://com.android.calendar/calendars"), new String[]{"calendar_id", "displayname"}, null, null, null);
  2. cursor.moveToFirst();
  3. // fetching calendars name
  4. String CNames[] = new String[cursor.getCount()];
  5. // fetching calendars id
  6. CId = new int[cursor.getCount()];
  7. for (int i = 0; i < CNames.length; i++)
  8. {
  9.          CId[i] = cursor.getInt(0);
  10.          CNames[i] = cursor.getString(1);
  11.          cursor.moveToNext();
  12. }
  13.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


2.) Adding an Event :

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ContentValues event = new ContentValues();
  2. // Calendar in which you want to add Evnet
  3. event.put("calendar_id", CalIds[0]);                                                  
  4. // Title of the Event    
  5. event.put("title", "Birthday");                                                          
  6. // Description of the Event
  7. event.put("description", "Birthday Party");                                  
  8. // Venue of the Event
  9. event.put("eventLocation", "Delhi");                                                  
  10. // Start Date of the Event  
  11. event.put("dtstart", StartDate);                                                          
  12. // End Date of the Event
  13. event.put("dtend", EndDate);                  
  14. // Event is all day                                            
  15. event.put("allDay", 1);                    
  16. // Set alarm on this Event                                              
  17. event.put("hasAlarm",1);                                                                  
  18. Uri eventsUri = Uri.parse("content://com.android.calendar/events");    
  19. // event is added
  20. getContentResolver().insert(eventsUri, event);
  21. cursor.close();
  22.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


3.) Deleting an Event :

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. //getContentResolver().delete(path to the content, want to delete, CONDITION, ARGUMENTS);
  2. //CONDITION + ARGUMENTS work as where condition to find a particular event.
  3. getContentResolver().delete(Uri.parse("content://com.android.calendar/events"), "calendar_id=? and description=? and eventLocation=? ", new String[]{String.valueOf(CalIds[0]), "Birthday Party", "Delhi"});
  4.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


If you want to do the same in 2.1, you need to do minor modification in above mentioned steps...

1.) Remains Same
2.) change path from "content://com.android.calendar/calendars" to "content://calendar/calendars"
3.) It should be like this :

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Uri CALENDAR_URI = Uri.parse("content://calendar/events");
  2. Cursor c = getContentResolver().query(CALENDAR_URI, null, null, null, null);
  3. if (c.moveToFirst())
  4. {
  5.         while (c.moveToNext())
  6.         {
  7.                 String desc = c.getString(c.getColumnIndex("description"));
  8.                 String location = c.getString(c.getColumnIndex("eventLocation"));                      
  9.                 // event id
  10.                 String id = c.getString(c.getColumnIndex("_id"));
  11.                 if ((desc==null) && (location == null))
  12.                 {
  13.                 }
  14.                 else
  15.                 {
  16.                         if (desc.equals("Birthday Party") && location.equals("Delhi"))
  17.                          {
  18.                                 Uri uri = ContentUris.withAppendedId(CALENDAR_URI, Integer.parseInt(id));
  19.                                 getContentResolver().delete(uri, null, null);
  20.                         }
  21.                 }
  22.         }
  23. }
  24.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


Hope this will help to understand calendar part easily.

Thanks
Kanika
Thanks
Kanika Rakheja
User avatar
techkanika
Experienced Developer
Experienced Developer
 
Posts: 83
Joined: Fri May 28, 2010 7:49 am

Top

Re: Insert, Delete events in Android Calendar...

Postby Menor » Wed May 25, 2011 8:24 pm

How the date works?

Could you post the source code of this example?

Thanks
Menor
Experienced Developer
Experienced Developer
 
Posts: 83
Joined: Mon Apr 11, 2011 3:45 pm

Re: Insert, Delete events in Android Calendar...

Postby techkanika » Mon Aug 08, 2011 7:55 pm

hi,

here is it's code :

Code: Select all

import java.util.Date;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.telephony.SmsManager;
import android.util.Log;

public class CalendarObserver extends ContentObserver
{
   
   private Context   context;
   Cursor         cur, curval;
   String         title, summ, loc;
   Date                 start, end;
   private      boolean   monitorStatus;
   private      int           contactCount, count, curCount;
   String         name, update;
   StringBuilder           sb;
   SmsManager      sms;
   
   public CalendarObserver(Handler handler, Context con)
   {
      super(handler);
      context = con;
   }
   
   public void startMonitoring()
   {
      Log.v(".............................................", "Entered in monitoring");
      monitorStatus = false;
      if (!monitorStatus)
      {
         curval = context.getContentResolver().query(Uri.parse("content://calendar/events"), new String[] { "title", "dtstart", "dtend", "description" }, null, null, null);
         if (curval != null && curval.getCount() > 0)
         {
            contactCount = curval.getCount();
         }
      }
   }
   
   @Override
   public boolean deliverSelfNotifications()
   {
      return true;
   }
   
   public void onChange(boolean selfChange)
   {
      super.onChange(selfChange);
            Thread thread = new Thread() {
               public void run()
               {
                  try
                  {
                     cur = context.getContentResolver().query(Uri.parse("content://calendar/events"), new String[] { "title", "dtstart", "dtend", "description" }, null, null, null);
                     curCount = 0;
                     if (cur != null && cur.getCount() > 0)
                     {
                        curCount = cur.getCount();
                     }
                     if ((curCount >= contactCount))
                     {
                        if (count == 3)
                        {
                           contactCount = curCount;
                           cur.moveToLast();
                           title = cur.getString(cur.getColumnIndex("title"));
                           start = new Date(cur.getLong(cur.getColumnIndex("dtstart")));
                           end = new Date(cur.getLong(cur.getColumnIndex("dtend")));
                           summ = cur.getString(cur.getColumnIndex("description"));
                           sb.append("New Event Added :\n");
                           sb.append("Title:");
                           sb.append(title + "\n");
                           sb.append("Start Time:");
                           sb.append(start + "\n");
                           sb.append("End Time:");
                           sb.append(end + "\n");
                           sb.append("Summary:");
                           sb.append(summ + "\n");
                        }
                     }
                     if ((curCount < contactCount))
                     {
                        Log.v(".............................................................", "Event deleted");
                     }
                  }
                  catch (Exception e)
                  {
                     e.printStackTrace();
                  }
               }
            };
            thread.start();
         }
      }
   }
}


I hope this will help you ...

kanika
Thanks
Kanika Rakheja
User avatar
techkanika
Experienced Developer
Experienced Developer
 
Posts: 83
Joined: Fri May 28, 2010 7:49 am

Re: Insert, Delete events in Android Calendar...

Postby matthelazy » Sat Aug 20, 2011 3:22 pm

Hi ! I'm new here and a beginner with android.
I'm trying to learn something about the calendar of my Samsung Galazy S2 (running Android 2.3.3).

I have tryed your code to "connect" to the calendar. But i obtain a stop on my device.
Do you know if your code is ok for 2.3 too ?
matthelazy
Once Poster
Once Poster
 
Posts: 1
Joined: Sat Aug 20, 2011 2:51 pm

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: Google [Bot] and 6 guests