gpt4 book ai didi

android - 服务中的谷歌驱动器上传和下载作为后台

转载 作者:太空狗 更新时间:2023-10-29 15:13:37 25 4
gpt4 key购买 nike

我可以使用在 Android 手机中添加的帐户上传到谷歌驱动器。我按照 http://www.youtube.com/watch?v=Ied1CjJ0iP0 中所述的步骤操作.我真正想做的是通过服务作为后台进程从多部手机上传到一个帐户。我该怎么做。

最佳答案

我首先使用 fragment 中的常规方法登录

    private fun upload() {
Log.i(TAG, "Start sign in")
val signInOptions = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(Drive.SCOPE_FILE)
.build()
val googleSignInClient = GoogleSignIn.getClient(this.requireActivity(), signInOptions)

startActivityForResult(googleSignInClient.signInIntent, MyFragment.REQUEST_CODE_SIGN_IN)
}


override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
MyFragment.REQUEST_CODE_SIGN_IN -> {
if (resultCode == Activity.RESULT_OK) {
Log.i(TAG, "Signed into drive ok.")

//Create an intent for starting the service
//Add whatever data you need to the intent
val intent = Intent(context, UploadService::class.java)
requireContext().startService(intent)
}
}
}

然后在上传服务中

override fun onHandleIntent(intent: Intent?) {
if (intent == null) {
return
}

val lastSignedInAccount = GoogleSignIn.getLastSignedInAccount(this) ?: throw IllegalStateException("Not logged in")
val driveClient = Drive.getDriveClient(this, lastSignedInAccount)
val driveResourceClient = Drive.getDriveResourceClient(this, lastSignedInAccount)

.... Do what you need to do here
}

记得将服务添加到您的 AndroidManifest.xml

 <service
android:name=".UploadService"
android:enabled="true"
android:exported="false" />

关于android - 服务中的谷歌驱动器上传和下载作为后台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14605946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com