3

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

4

1 回答 1

0

我首先使用我的片段中的正常方法登录来做到这一点

    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)
            }
        }
    }

然后在 UploadService

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" />
于 2018-10-12T08:57:07.950 回答