Sunday, March 18, 2012

How to add event,reminder in Android 2.3 Calendar through code?


First of all we need to get the calendar provider uri.........
if (Build.VERSION.SDK_INT >= 8 ) {
        l_eventUri = Uri.parse("content://com.android.calendar/events");
    } else {
        l_eventUri = Uri.parse("content://calendar/events");
    }
But below the getCalendarUriBase(this) method is already return to get URI.So no need to use above code.It is just for your understanding.


You'll also need to add these permission to your manifest for this method.........
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />

/*
* A device may have more than one Calendar configure in it. So first,
* you must find out available calendar's name and their id.
 */
Cursor cursor = getContentResolver() .query(Uri.parse(getCalendarUriBase(this) ),new String[] { "calendar_id", "displayname" }, null,null, null);
cursor.moveToFirst();
// fetching calendars name
String CNames[] = new String[cursor.getCount()];
// fetching calendars id
int[] CalIds = new int[cursor.getCount()];
for (int i = 0; i < CNames.length; i++) {
CalIds[i] = cursor.getInt(0);
CNames[i] = cursor.getString(1);
cursor.moveToNext();
}



        // get calendar
        Calendar cal = Calendar.getInstance();     
        Uri EVENTS_URI = Uri.parse(getCalendarUriBase(this) + "events");
        ContentResolver cr = getContentResolver();


        // event insert
        ContentValues values = new ContentValues();
        values.put("calendar_id",CalIds);
        values.put("title", "Reminder Title");
        values.put("allDay", 0);
        values.put("dtstart", cal.getTimeInMillis() + 11*60*1000); // event starts at 11 minutes from now
        values.put("dtend", cal.getTimeInMillis()+60*60*1000); // ends 60 minutes from now
        values.put("description", "Reminder description");
        //Event Id
        values.put("_id", scheduleId);  
        //0~ default; 1~ confidential; 2~ private; 3~ public
        values.put("visibility", 0);
        //0~ false; 1~ true
        values.put("hasAlarm", 1);
        //status: 0~ tentative; 1~ confirmed; 2~ canceled
        values.put("eventStatus", 1);
    //0~ opaque, no timing conflict is allowed; 1~ transparency, allow overlap of scheduling
        values.put("transparency", 0);
        Uri event = cr.insert(EVENTS_URI, values);


        // reminder insert
        Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(this) + "reminders");
        values = new ContentValues();
        values.put( "event_id", Long.parseLong(event.getLastPathSegment()));
        values.put( "method", 1 );
        values.put( "minutes", 10 );
        cr.insert( REMINDERS_URI, values );



        // for deleting event
// getContentResolver().delete(path to the content, want to delete,
// CONDITION, ARGUMENTS);
        // CONDITION + ARGUMENTS work as where condition to find a particular
 event.

getContentResolver().delete(
Uri.parse(getCalendarUriBase(this)),
"calendar_id=? and description=? and eventLocation=? ",
new String[] { String.valueOf(CalIds[0]), "Birthday Party",
"Delhi" });


getCalendarUriBase(this) is the method which provides the ContentProvider uri of Android device base on versions...........which is given below....



 private String getCalendarUriBase(Activity act) {
        String calendarUriBase = null;
        Uri calendars = Uri.parse("content://calendar/calendars");
        Cursor managedCursor = null;
        try {
            managedCursor = act.managedQuery(calendars, null, null, null, null);
        } catch (Exception e) {
        }
        if (managedCursor != null) {
            calendarUriBase = "content://calendar/";
        } else {
            calendars = Uri.parse("content://com.android.calendar/calendars");
            try {
                managedCursor = act.managedQuery(calendars, null, null, null, null);
            } catch (Exception e) {
            }
            if (managedCursor != null) {
                calendarUriBase = "content://com.android.calendar/";
            }
        }
        return calendarUriBase;
    }








20 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. The NCAA Division I Men's Basketball Tournament is a single-elimination competition played each spring in the United States, currently featuring 68 college basketball teams from the Division I level of the National Collegiate Athletic Association (NCAA), to establish the national championship. The event was produced in 1939 by the National Association of Basketball Coaches, as well as was the concept of Ohio State University train Harold Olsen. [1] Played mostly during March, it is understood informally as March Madness or the Big Dance, as well as has become one of one of the most famous yearly showing off occasions in the United States.

    March Madness

    March Madness Live

    March Madness Live Stream

    March Madness 2017

    March Madness 2017 Live

    NCAA March Madness

    2017 March Madness

    March Madness Bracket


    The competition groups consist of champions from 32 Division I seminars (which obtain automated quotes), and 36 groups which are awarded at-large berths. These "at-large" groups are chosen by an NCAA option committee, then introduced in a country wide televised occasion on the Sunday preceding the First Four play-in games, currently held in Dayton, Ohio, as well as referred to as Selection Sunday. The 68 groups are separated right into four areas and also arranged into a solitary removal " brace", which pre-determines, when a group wins a video game, which team it will encounter next. Each team is "seeded", or ranked, within its area from 1 to 16. After an initial four video games in between 8 lower-ranked groups, the competition happens throughout the program of three weekends, at pre-selected neutral sites across the United States. Teams, seeded by ranking, proceed with a solitary video game removal bracket starting with a first round including 64 groups, to a "Sweet Sixteen", and for the last weekend break of the tournament, a Final Four. The Final Four is usually played throughout the initial weekend break of April. These 4 teams, one from each area (East, South, Midwest, and also West), complete in one area for the championship game.

    ReplyDelete

  3. The spending comes as competition for public research money is fierce and expected to get fiercer, with funding for the NIH expected to plateau and even drop in coming years.


    March Madness

    March Madness Live

    March Madness Live Stream

    March Madness 2017

    March Madness 2017 Live

    ncaa March Madness

    ncaa march madness live

    ncaa tournament

    March Madness Bracket

    ncaa final four




    NCCAM's continuing interest in acupuncture comes even though many of its studies have found that acupuncture and similar therapies work no better than a sham treatment at easing symptoms like pain and fatigue.

    ReplyDelete
  4. Idaho: Despite the fact that the Tar Pumps displaced within the ACC event semifinals, they had Duke over the rules with regard to a lot of the pioneer 50 percent. Justin Fitzgibbons, with formulated some efficient circumference game, and a foul-free Joel Acai berry II include the first considerations.
    March Madness

    March Madness Live

    March Madness Live Stream

    March Madness 2017

    Certainly, we know Indicate Few has never created a last Five. The following group, stocked by means of athletic scorers that can additionally lck lower competitors, are going to be your partner's first
    March Madness 2017 Live

    ncaa March Madness

    ncaa march madness live

    ncaa tournament

    March Madness Bracket

    ncaa final four

    Whether you’re even now relating to the containment system this 8-9 sport, and would like some sort of consensus feeling upon your Final Five picks, we’ve became most people dealt with which includes a position of their 68 clubs in the competition. Anyone can with certainty create your choice with some of those mid-major darlings you’ve recently been eyeing for worried likely.
    March Madness

    March Madness Live

    March Madness Live Stream

    March Madness 2017

    March Madness 2017 Live

    ncaa March Madness

    ncaa march madness live

    ncaa tournament

    March Madness Bracket

    ncaa final four

    Villanova: The superior general seed products. Despite the team’s position inside most challenging region (the East, by means of matchups next to Fight it out in addition to SMU looming), Villanova’s three-man unpleasant center with Jalen Brunson, Josh Hart together with Kris Jenkins gets the possible to repeat when domestic winners.

    ReplyDelete
  5. Listen to ladygaga | SoundCloud is an audio platform that lets you listen to what you love and share the sounds you create..Lady gaga wiki 321 Tracks. 1145579 Followers. Stream Tracks and Playlists from ladygaga on your desktop or mobile device.

    ReplyDelete
  6. Watch Annihilation full movie online, Watch Annihilation loading online, Watch Annihilation loading free, Watch Annihilation loading vostfr, Watch Annihilation loading ita, Watch Annihilation loading Watch justice-league (2018) Full Online HD Movie Loading Free Download How exactly to Watch ...


    Annihilation watch full movie online

    ReplyDelete

  7. At the point when are the genuine SAG Awards?

    Sag Awards Live

    ReplyDelete