简体   繁体   English

将同步事件添加到Google日历

[英]Add synced events to google calendar

I've created an android app that adds events to any of my google calendars. 我创建了一个Android应用,可将事件添加到我的任何Google日历中。 It works so far but the events that I create are only visible in my local calendar app and not on the web. 到目前为止,它仍然有效,但是我创建的事件仅在本地日历应用程序中可见,而在网络上不可见。 Here is the code I use to insert the events: 这是我用来插入事件的代码:

ContentResolver contentResolver = getContentResolver();
for (Date date : getDates()) {
    ContentValues values = new ContentValues();
    values.put(Events.CALENDAR_ID, calendar.getId());
    values.put(Events.TITLE, type.getName());
    values.put(Events.DTSTART, buildStartDate(date));
    values.put(Events.DTEND, buildEndDate(date));
    values.put(Events.ALL_DAY, 0);
    values.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
    Uri event = contentResolver.insert(
                    CalendarContract.Events.CONTENT_URI, values);
    Toast.makeText(getApplicationContext(),
                    "Inserted event: " + event.toString(),
                    Toast.LENGTH_LONG).show();
}

The caledar.getId() returns the Id of the calendar I've selected previously. caledar.getId()返回我之前选择的日历的ID。 buildStartDate and buildEndDate creates the start and the end date and returns it as milli seconds. buildStartDatebuildEndDate创建开始日期和结束日期,并以毫秒为单位返回。 The type.getName() returns just a string that is used as the title. type.getName()仅返回用作标题的字符串。

Does anyone know why the events are only displayed in my local calendar app? 有谁知道为什么事件仅显示在我的本地日历应用程序中? When I use the calendar app to insert a new event, it is synced to the web, so the syncing does work. 当我使用日历应用程序插入新事件时,它会同步到网络,因此同步确实可行。

I can't see your SyncAdapter . 我看不到您的SyncAdapter It is required. 它是必需的。 The following code may help you. 以下代码可以为您提供帮助。

static Uri asSyncAdapter(Uri uri, String account, String accountType) {
return uri.buildUpon()
    .appendQueryParameter(android.provider.CalendarContract.CALLER_IS_SYNCADAPTER,"true")
    .appendQueryParameter(Calendars.ACCOUNT_NAME, account)
    .appendQueryParameter(Calendars.ACCOUNT_TYPE, accountType).build();

} }

See this link and also this . 看到这个链接 ,也这个

Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM