读写日历

时间:2010-06-23 12:31:23

标签: android calendar

我的目标是阅读和书写日历。

我能够从内容中读取数据:// 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());

  }

 }

我也在日历中插入新记录,如:

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

但它没有出现在日历中。

当我要在日历中插入新事件时:

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

抛出该列无效的异常。

这怎么可能。 感谢

3 个答案:

答案 0 :(得分:7)

日历内容提供商不是Android SDK的一部分。它之前的Android版本之间已经发生了变化,并将再次发布。它可能无法在某些设备上运行,因为它们已经用自己的替换默认日历应用程序。

Do not use undocumented content providers.

解决方案与the question you asked 32 minutes previously相同 - 使用Google Calendar GData API来操作用户的日历。

答案 1 :(得分:0)

//要将事件插入到Android 2.2及更高版本的日历中,如果少于2.2而不是内容://com.android.calendar写入内容:// calendar

String calUriString = "content://com.android.calendar/events";
            ContentValues values = new ContentValues();

values.put("calendar_id",2); //id, We need to choose from our mobile for primary its 1
values.put("title", "Birthday");
values.put("description", "Go home at 2pm");
values.put("eventLocation", "Home");
long startTime = System.currentTimeMillis() + 1000 * 60 * 60*24; // Next day

values.put("dtstart", startTime);
values.put("dtend", startTime);

values.put("allDay", 1); //If it is bithday alarm or such kind (which should remind me for whole day) 0 for false, 1 for true

values.put("eventStatus", 1); // This information is sufficient for most entries tentative (0), confirmed (1) or canceled (2):

values.put("visibility", 3); // visibility to default (0), confidential (1), private (2), or public (3):

values.put("transparency", 0); // You can control whether an event consumes time  opaque (0) or transparent (1).

values.put("hasAlarm", 1); // 0 for false, 1 for true


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

答案 2 :(得分:0)

自第一个问题以来已经过了日期。今天似乎日历内容提供商已有详细记录。 (也许自API 14以来?)

对于异常的简短回答是您为列提供的类型与预期类型的​​不同。 (dtstart而不是String的长期预期)

http://developer.android.com/reference/android/provider/CalendarContract.EventsColumns.html#DTSTART

有关更多信息,developer.android.com上还有另一个资源: http://developer.android.com/guide/topics/providers/calendar-provider.html