Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 谷歌硬盘上传和下载作为后台服务_Android_Google Drive Api - Fatal编程技术网

Android 谷歌硬盘上传和下载作为后台服务

Android 谷歌硬盘上传和下载作为后台服务,android,google-drive-api,Android,Google Drive Api,我可以使用安卓手机中添加的帐号上传到谷歌硬盘上。我真正想做的是通过一个服务作为后台进程从多部手机上传到一个账户。我该怎么做 我是通过使用片段中的普通方法首次登录来实现的 private fun upload() { Log.i(TAG, "Start sign in") val signInOptions = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)

我可以使用安卓手机中添加的帐号上传到谷歌硬盘上。我真正想做的是通过一个服务作为后台进程从多部手机上传到一个账户。我该怎么做

我是通过使用片段中的普通方法首次登录来实现的

    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" />