Android日历:如何为日历INSERT编写同步适配器 [英] Android Calendar: how to write sync adapter for calendar INSERT

查看:120
本文介绍了Android日历:如何为日历INSERT编写同步适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上想重新打开以下未答复的帖子:

I would basically like to reopen the following unanswered post:

我想利用Android Calendar Provider API创建日历 并将事件插入其中.创建的日历不能是本地副本. 它必须与Google(在线)日历同步.该文件说 为此,我必须使用同步适配器.但是如何编写这样的同步适配器?

I would like to make use of the Android Calendar Provider API to create a calendar and insert events into it. The calendar that is created must not be a local copy. It must synchronize with the Google (online) Calendar. The documentation says in order to do this I must use a sync adapter. But how do I write such a sync adapter?

到目前为止,我发现了以下两个帖子,但没有解决方案.

So far I've found the two following posts but no solution.

  • Create new synced calendar with android api
  • How can I add a new phone calendar to Android?

我想为此使用Android Calendar API,而不要使用Google API,以便 用户可以离线工作.

I want to use the Android Calendar API for this and not the Google API so that users can work offline.

推荐答案

在这方面,Google似乎弄乱了CalendarContract.文档尚不完全清楚:

It looks like Google messed up CalendarContract in this regard. The documentation is not completely clear about it:

...应 插入新日历作为同步适配器.

...inserting new calendars should be done as a sync adapter.

这句话的表达方式对我来说似乎是行之有效的机会.但是实际上,使用CalendarContract(判断您所链接的其他SO问题)的应用程序无法创建同步日历.

The way this is phrased appears to me like there is the slim chance it might work. But practically speaking (judging your linked other SO questions) an app using CalendarContract cannot create synchronized calendars.

您真的不想经历创建自己的SyncAdapter的所有麻烦.我为我的一个应用程序做了一次,因为我想要一个可靠的解决方案,以便在Ice Cream Sandwich之前的设备(尚不存在CalendarContract)上实现同步日历访问.

You really do not want to go through all that trouble of creating your own SyncAdapter. I did it once for one of my apps because I wanted a reliable solution for having synchronized calendar access on pre Ice Cream Sandwich devices (where CalendarContract doesn't exist, yet).

这是很多工作.它涉及到与Google Calendar Web服务API的接口(同时遵守其访问限制),保留数据的本地副本并仅同步差异.最终,您自己的SyncAdapter还将复制已经存在的Google Calendar SyncAdapter的数据.

That's a lot of work. It involves interfacing with the Google Calendar web service API (while respecting their access limitations), keeping a local copy of the data and synchronizing just the differences. Ultimately your own SyncAdapter would also duplicate the data of the already existing Google Calendar SyncAdapter.

作为解决方法,您可以直接在 Google日历在线API . 它要求您的应用通过android.permission.USE_CREDENTIALS要求用户授权.

What you could do as a workaround is just implementing the necessary "create calendar" function directly on the Google Calendar online API. It requires that your app asks the user for authorization via android.permission.USE_CREDENTIALS.

AccountManager 可用于获取用于访问的正确授权令牌Google Calendar API.方法getAuthToken需要auth令牌类型参数.日历读/写访问的正确方法是以下字符串文字:"Manage your calendars".

AccountManager can be used for getting the correct authorization token for accessing the Google Calendar API. The method getAuthToken requires an auth token type parameter. The correct one for calendar read / write access is the following string literal: "Manage your calendars".

该解决方法有两个大问题:

There are two big problems with that workaround though:

  1. 您无法确定日历同步后端是否使用Google日历.不幸的是,这是一个黑匣子.

  1. You cannot know for sure if the calendar synchronization backend makes use of Google Calendar. It's unfortunately a black box.

您无法确定日历同步适配器何时会赶上这些更改.我想有可能以某种方式强制重新同步.但是即使那样,您的应用也将停滞不前.您不能仅基于该新日历进行操作并立即使用它.您的应用必须等待直到同步发生.

You cannot know for sure when the calendar sync adapter is going to catch up with these changes. I guess it is possible to force a re-sync somehow. But even then, your app will be stalled. You cannot just operate based on that new calendar and use it right away. Your app must wait until the synchronization happened.

您可以为此查询CalendarContract.让您的应用等待并在后台线程中查找更改,直到出现新日历为止.然后,您有了它的本地ID,然后继续.

You could poll CalendarContract for that. Let your app wait and look for changes in a background thread until the new calendar shows up. Then you have its local id and move on.

这篇关于Android日历:如何为日历INSERT编写同步适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆