编辑
在Android上,没有本地/默认/开箱即用的机制来实时同步数据。
不过有替代方案。
在Android环境下,出现了两种主要选择:
使用带有某些持久存储的套接字 - 您可以实现支持套接字的后端服务器,并将此支持添加到您的android(ios / web / desktop)应用程序中。这是实现设备之间实时同步的最常见和相当低级的机制。优点:跨平台通用、行业标准、在线信息丰富。 缺点:因为它是相对较低级别的东西,需要大量代码,如果有许多同步事件,则可能相当复杂和复杂,需要后端,需要单独的持久性。主要实现:Socket.io
Firebase 实时数据库/Cloud Firestore - 基本上是套接字的包装器,结合一些数据库。“优点”:既不需要后端也不需要数据库,在原型/低负载应用程序中免费,非常直观的实现,跨平台,运作得很好。 缺点:如果您想走“干净的方式”,需要大量样板文件,您无法控制内部进程,高负载时不太明显的付款计算,您将链接到具有特定API的第三方后端 - 如果您想从中迁移到其他地方而不需要完全重做应用程序,则需要定义良好的架构。 比较:firebase云Firestore与实时数据库
我不会详细介绍实现细节,因为官方文档在这种情况下相当简单(不像谷歌驱动器)
Cloud Firestore
文档
Realtime Database
文档
编辑结束
是的。您可以使用Google Drive
私下存储数据。它只能由您的应用程序访问。此外,它不会占用用户Google Drive
中的任何空间,这非常方便。在这种情况下,跨平台是可能的,但需要分别为iOs和Android实现(SDK不支持开箱即用)。
因此,解决方案基于Google Drive API,更具体地说是其存储应用程序特定数据部分。
用户唯一可见的操作就是谷歌登录提示。
这里有一个详细的Java快速入门指南。
因此,为了完成此操作,您需要:
添加依赖项;
按照这里和这里所述实现谷歌认证
非常重要 使用drive.appdata
和drive.file
范围对用户进行身份验证。此外,在Google Cloud控制台中启用Google Drive API。所有这些都在这里中描述。
找出您的Room SQLite
文件所在位置,如下所示:String currentDBPath = context.getDatabasePath("your_database_name.db").getAbsolutePath();
像这里所描述的那样将文件上传到Google Drive应用程序文件夹中
基本上看起来像这样:
在控制台中启用Google Drive API
添加依赖项(可能需要更新到相关版本)
implementation 'com.google.android.gms:play-services-auth:17.0.0'
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'
}
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
- 设置 Google Drive 客户端服务(上下文位置)
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()) {
}
您可以在这里获取更多相关信息。
希望能对您有所帮助。