What is the location of the calendar in Android 4.0?

When I was developing an APP whose API is 7 on my Nexus S, there was no problem creating a new event on my calendar.

I used this 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 arose when I upgraded my Nexus S to Android ICS 4.0. Without changing the code, I got an error. On the logarithm, I could read:

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

Of course, the cursor is null. Maybe any changes to the calendar database? So, I would like to know how I can create new calendar events developing an API 7 application on Android 4.0

Thanks;)

+4
source share
2 answers

The calendar provider is new for Android 4.0, so you can view the documentation:

http://developer.android.com/guide/topics/providers/calendar-provider.html

For versions prior to Froyo, I know that the calendar was in content://calendar/calendars , but it has changed since then.

+3
source

In Android 4.0, the calendar is in the same Uri as in Android 2.3. Therefore, I add 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 information at: http://developer.android.com/guide/topics/providers/calendar-provider.html , as John said.

+5
source

Source: https://habr.com/ru/post/1387425/


All Articles