如何将活动添加到Google日历中?

时间:2018-07-06 07:42:52

标签: android api calendar

我正在创建事件并将Android中的事件添加到google calendar应用中,但事件未显示在com.google.calendar中。 我想同时在我的设备日历和Google日历中显示该事件;

这是我的代码

    ContentResolver cr = this.getContentResolver();
    String eventUriString = "content://com.android.calendar/events";
    ContentValues eventValues = new ContentValues();

    eventValues.put("calendar_id", 1);
   //eventValues.put(CalendarContract.Events.CALENDAR_ID,"sofwrengr@gmail.com");
    eventValues.put("title", "teachease");
    eventValues.put("description", "this is my event");
    eventValues.put("eventLocation", "peshawar");
    eventValues.put(CalendarContract.Events.DTSTART, startTime);
    eventValues.put(CalendarContract.Events.DTEND, endTime + 60 * 60 * 1000);
    eventValues.put("eventStatus", "abdullah");
    eventValues.put("hasAlarm", 1);


    eventValues.put("eventTimezone", TimeZone.getDefault().getID());
    Uri eventUri = cr.insert(Uri.parse(eventUriString), eventValues);
    long eventID = Long.parseLong(eventUri.getLastPathSegment());

1 个答案:

答案 0 :(得分:2)

您可以按照docs

的说明使用Intent向Google日历添加事件。

意图示例:

public void addEvent(String title, String location, long begin, long end) {
Intent intent = new Intent(Intent.ACTION_INSERT)
        .setData(Events.CONTENT_URI)
        .putExtra(Events.TITLE, title)
        .putExtra(Events.EVENT_LOCATION, location)
        .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, begin)
        .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, end);
  if (intent.resolveActivity(getPackageManager()) != null) {
    startActivity(intent);
  }
}

意图过滤器示例:

<activity ...>
  <intent-filter>
    <action android:name="android.intent.action.INSERT" />
    <data android:mimeType="vnd.android.cursor.dir/event" />
    <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>
</activity>