Android-使用适用于Android的Google Drive API

Dan*_*ele 5 android google-drive-android-api

For my App I need to have a List sync with Google Drive. I have already implemented the SignIn and had my Main_Activity implement both:

com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks,
com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener
Run Code Online (Sandbox Code Playgroud)

Even though I read the whole Google Drive API for Android documentation, and more specifically the Store Application Data part. And looked through the example on GitHub, I wasn't able to get it to work. I personally think that this documentation is really confusing to read. It's not even clear what is the difference between the Google Drive API for android and the Google Drive REST API, and which one I should use for my scenario.

Also I noticed that the example on GitHub extends a custom Activity that has other methods in it.

Can any of you explain step by step how to use the android drive API?

Che*_*amp 3

我很熟悉你的沮丧。有“Google Drive Android API”(GDAA) 和“Drive REST API”。网络上有一些很好的文档,但是找到它并理解它可能是一个挑战,特别是因为包的名称非常相似。此外,“Drive REST API”至少有两个现有版本,您必须保持版本一致。

关于GDAA,你已经找到了这个文档,但是你应该仔细看看。我建议您做一个简单的活动,例如创建一个文件并从那里开始工作。

我认为您引用的 GitHub 上示例中的自定义 Activity 是BaseDemoActivity. 该类仅提供一些生命周期方法和其他一些常见例程。

Google Drive Android API (GDAA) 与 Play 服务紧密集成,Google 声称可以提供更好的性能。(请参阅此处的注释):

注意:本快速入门说明了如何在 Android 应用程序中使用 Drive REST API。然而,在大多数情况下,生产版 Drive 应用程序将通过使用适用于 Android 的 Drive API 受益匪浅,该 API 与 Google Play 服务集成并提供更好的性能。在您的 Android 应用程序中使用 Drive REST API 之前,您应该仔细查看适用于 Android 的 Drive API,并在可能的情况下在您的应用程序中使用它。如果您想了解更多信息,可以使用适用于 Android 的 Drive API 快速入门。

尽管如此,由于同步频率的限制越来越严格,我在大多数情况下都放弃了 GDAA。(有关更多详细信息,请参阅本文底部的注释。)

使用 GDAA 时,需要记住的一件关键事情是,即使您的代码可以在 UI 线程上运行,GDAA 也不能,因为它代表您执行的任务可能很长。这意味着一旦您请求 GDAA 从 UI 线程完成某些任务,GDAA 将在后台(而不是在 UI 线程上)完成该工作,并通过回调将结果传递给您。

这种结构虽然是必要的,但意味着您的代码将是由 GDAA 调用的一系列方法,并且不一定会展示您可能习惯的清晰的顺序格式。我将其视为软件中的弹球机。

虽然不是一步一步的说明,但我希望这有助于为您指明正确的方向。


关于同步频率: 更具体地说,将按照指定的方式上传到服务器DrivePreferencesApi。上传通常相当快。然而,下载是有速率限制的。请参阅此文档

为了避免设备和服务器负载过大,同步请求受到速率限制。在这种情况下,操作将失败并显示 DRIVE_RATE_LIMIT_EXCEEDED 状态,这表明最近已经发生过同步,因此不需要再次同步。在足够的退避持续时间后重新尝试时,该操作将成功。

我相信“退避持续时间”取决于安装的 Play 服务的版本。根据我的经验,这个持续时间从几分钟到 0.5 小时或更长不等。这可能已经改变,我试图找到这方面的文档,但没有成功。

如果 GDAA 的下载限制不适合您,那么您可能需要考虑 Drive REST。您还可以将 Firebase 作为可能的解决方案。