Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read and write Calendar

My objective is to read and write Calendar.

i am able to read data from the content://calendar/calendars and content://calendar/events

String uriString = "content://calendar/calendars";
  Log.i("INFO", "Reading content from " + uriString);
  readContent(uriString);
  uriString = "content://calendar/events";
  Log.i("INFO", "Reading content from " + uriString);
  readContent(uriString);

private void readContent(String uriString) {

  Uri uri = Uri.parse(uriString);
  Cursor cursor = mContext.getContentResolver().query(uri, null, null,
    null, null);
  if (cursor != null && cursor.getCount() > 0) {
   cursor.moveToFirst();
   String columnNames[] = cursor.getColumnNames();
   String value = "";
   String colNamesString = "";
   do {
    value = "";

    for (String colName : columnNames) {
     value += colName + " = ";
     value += cursor.getString(cursor.getColumnIndex(colName))
       + " ||";
    }

    Log.e("INFO : ", value);
   } while (cursor.moveToNext());

  }

 }

i am also inserting new record in the calendar like :

String calUriString = "content://calendar/calendars";
   ContentValues values = new ContentValues();
   values.put("name", "Code Generate Calendar");
   values.put("displayName", "Code Generate Calendar");
   values.put("hidden", 0);
   values.put("color", "-7581685");
   values.put("access_level", "700");
   values.put("selected", "1");
   values.put("timezone", "Asia/Karachi");

   Uri calendarUri = context.getContentResolver().insert(
   Uri.parse(calUriString), values);

but it is not appearing in the Calendar.

when i going to insert new events in Calendar like :

   ContentValues values = new ContentValues();
   values.put("calendar_id", 4);
   values.put("dtend", "1277337600000");
   values.put("dtstart", "1277251200000");
   // values.put("title", "first TEst event");
   values.put("transparency", 1);
   values.put("selected", 1);
   values.put("color", "-16380578");
   // values.put("lastDate", "6/25/2010");
   //values.put("access_level", 700);
   values.put("eventStatus", 1);
   values.put("eventTimezone", "UTC");
   values.put("timezone", "Asia/Karachi");
   values.put("allDay", 1);
   String eventUriString = "content://calendar/events";
   Uri eventUri = context.getContentResolver().insert(
   Uri.parse(eventUriString), values);

throwing exception that column is invalid.

how this possible. Thanks

like image 752
Kamran Omar Avatar asked Jun 30 '26 09:06

Kamran Omar


1 Answers

The calendar content provider is not part of the Android SDK. It has changed between Android releases before and will do so again. It may not work on some devices where they have replaced the default calendar application with their own.

Do not use undocumented content providers.

The solution is the same as the question you asked 32 minutes previously -- use the Google Calendar GData APIs to manipulate the user's calendar.

like image 166
CommonsWare Avatar answered Jul 01 '26 22:07

CommonsWare



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!