我正在开发一个在本地存储用户数据的应用程序。我用过Room
数据库将用户创建的数据存储在 Android 设备中。现在,如果用户拥有多于一台设备,那么数据应该可以在他们的所有 (Android) 设备上使用,而无需任何中央数据库或 Web 服务。
注意:对于 iOS 端,我使用了 CloudKit
在 iOS 设备之间同步数据。有没有办法对 Android 设备做同样的事情?请注意,我不想将数据存储在任何中央数据库或网络服务器或任何中央云存储上。它应该只归用户所有。 (即它应该通过用户的 Google 帐户同步)。
注意:我想要一个原生 android 解决方案,并且不需要跨平台(Android 到 iOS)同步。
更新:到目前为止,我发现了一些选项,例如 Firestore和 Realtime database一些 AWS 服务也提供同步功能,但所有这些的问题是它们都将用户数据存储在一个中央云位置。
所以我的问题还是一样 - 有没有办法使用 Google Drive
在 android 设备之间同步数据?或类似的东西,我们可以利用用户自己的云存储,而不是将用户数据存储在中央数据库中。
最佳答案
编辑
有没有在 上实时同步数据的原生/默认/开箱即用机制安卓
不过也有代言人。
在 Android 的上下文中,出现了两个主要选项:
我不会详细介绍实现的细节,因为在这种情况下官方文档非常简单(与谷歌驱动器不同)
Cloud Firestore
文件Realtime Database
文件编辑结束
是的。可以使用
Google Drive
私下存储数据.它只能由您的应用程序访问。而且它不会占用用户中的任何空间Google Drive
这很方便。在这种情况下可以跨平台,但需要为 iOs 和 Android 单独实现(SDK 不支持开箱即用)。所以解决方案是基于Google Drive API更具体地说是 Store application-specific data一部分。
与此相关的用户操作唯一可见的是 google 登录提示。
Here是一个详细的 Java 快速入门
因此,为了做到这一点,您将需要:
drive.appdata
对用户进行身份验证和 drive.file
范围。还有enable Google Cloud Console 中的 Google Drive API。所有这些都描述了here Room SQLite
文件的位置如下:String currentDBPath = context.getDatabasePath("your_database_name.db").getAbsolutePath();
基本上它看起来像这样
implementation 'com.google.android.gms:play-services-auth:17.0.0'// for google sign in
// for drive integration
implementation 'com.google.android.gms:play-services-auth:16.0.1'
implementation 'com.google.http-client:google-http-client-gson:1.26.0'
implementation('com.google.api-client:google-api-client-android:1.26.0') {
exclude group: 'org.apache.httpcomponents'
}
implementation('com.google.apis:google-api-services-drive:v3-rev136-1.25.0')
{
exclude group: 'org.apache.httpcomponents'
}
android
毕业标签 packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/notice.txt'
exclude 'META-INF/ASL2.0'
}
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(
Scope(Scopes.DRIVE_FILE),
Scope(Scopes.DRIVE_APPFOLDER)
)
.requestEmail()
.build()
googleSingInClient = GoogleSignIn.getClient(this, gso)
val signInIntent = googleSingInClient.signInIntent
startActivityForResult(signInIntent, RC_SIGN_IN)
... // handle sign in result
val mAccount = GoogleSignIn.getLastSignedInAccount(this)
val credential =
GoogleAccountCredential.usingOAuth2(
applicationContext, Collections.singleton(Scopes.DRIVE_FILE)
)
credential.setSelectedAccount(mAccount.getAccount())
val googleDriveService =
Drive.Builder(
AndroidHttp.newCompatibleTransport(),
GsonFactory(),
credential
)
.setApplicationName("Your app name")
.build()
val fileMetadata = File()
fileMetadata.setName("your_database_name.db")
fileMetadata.setParents(Collections.singletonList("appDataFolder"))
val databasePath = context.getDatabasePath("your_database_name.db").absolutePath
val filePath = File(databasePath)
val mediaContent = FileContent("application/sqlite", filePath)
val file = driveService.files().create(fileMetadata, mediaContent)
.setFields("id")
.execute()
val files = driveService.files().list()
.setSpaces("appDataFolder")
.setFields("nextPageToken, files(id, name)")
.setPageSize(10)
.execute()
for (file in files.getFiles()) {
// get file here
}
更多信息,您可能会得到 here
希望能帮助到你。
关于Android:如何在同一用户的设备之间同步存储在 Room 数据库中的应用程序数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61155034/