Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is the location of calendar in Android 4.0?

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:

  • no such column:displayname, db=/data/data/com.android.providers.calendar/databases/calendar.db

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 ;)

like image 566
ElChencho Avatar asked Feb 23 '23 01:02

ElChencho


2 Answers

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.

like image 101
ElChencho Avatar answered Mar 03 '23 13:03

ElChencho


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.

like image 36
John Giotta Avatar answered Mar 03 '23 13:03

John Giotta