【问题标题】:Android open google drive with intent with specified accountAndroid使用指定帐户意图打开谷歌驱动器
【发布时间】:2018-11-02 01:40:02
【问题描述】:

我在我的 Google 云端硬盘中设置了多个帐户

account1@gmail.com account2@gmail.com

我想通过意图用account2@gmail.com 打开谷歌驱动器。 我可以使用以下功能打开 Google Drive 应用程序。

fun startOpenGoogleDriveApp() {
    try {
        val intent = activity.packageManager.getLaunchIntentForPackage("com.google.android.apps.docs")
        startActivity(intent)
    }catch (e:Exception){
        e.printStackTrace()
    }
}

尝试使用intent.putExtra(Intent.EXTRA_USER,"account2@gmail.com"),但没有成功。

是否可以在 Intent Extras 中发送/指定帐户?非常感谢您的帮助。

【问题讨论】:

  • 您可以参考这个post。您可能需要创建一个服务帐户,如 this 文档中所述。同样来自此page,如果您不希望用户交互并希望为您的应用程序创建一个 Google Drive 帐户来提供文件,您可以将加密的访问令牌和刷新令牌嵌入到您的应用程序中,并使用以下命令初始化凭证对象嵌入的令牌。
  • 请斯里,检查我的答案。我曾经也遇到过这个问题。我发布了我从研究中了解到的所有信息。

标签: android android-intent kotlin google-drive-android-api


【解决方案1】:

我既不熟悉kotlin,也不熟悉您使用的方法。我正在告诉您文档中确切提到并且我熟悉的方式。

//Starts the sign-in process and initializes the Drive client.
    public void signIn() {
        GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestScopes(Drive.SCOPE_FILE)
                .requestScopes(Drive.SCOPE_APPFOLDER)
                .build();
        GoogleSignInClient googleSignInClient = GoogleSignIn.getClient(this, signInOptions);
        startActivityForResult(googleSignInClient.getSignInIntent(), REQUEST_CODE_SIGN_IN);
    }

/**
 * Handles resolution callbacks.
 */
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_CODE_SIGN_IN) {
        if (resultCode == RESULT_OK) {
            initializeDriveClient(GoogleSignIn.getLastSignedInAccount(this));
        } else if (resultCode == RESULT_CANCELED) {
            Snackbar.make(findViewById(R.id.fab), R.string.sign_in_alert, Snackbar.LENGTH_SHORT).show();
        }else{
            Snackbar.make(findViewById(R.id.fab), R.string.sign_fail, Snackbar.LENGTH_SHORT).show();
        }
    }
}

/**
 * Continues the sign-in process, initializing the DriveResourceClient with the current
 * user's account.
 */
private void initializeDriveClient(GoogleSignInAccount signInAccount) {
    mDriveResourceClient = Drive.getDriveResourceClient(getApplicationContext(), signInAccount);
    mDriveResourceClient.getAppFolder().addOnSuccessListener(new OnSuccessListener<DriveFolder>() {
        @Override
        public void onSuccess(DriveFolder driveFolder) {
            onDriveClientReady();
            // CONTINUE THE TASK
        }
    });
}

abstract void onDriveClientReady();

现在,把它放在一个abstract 活动类中,然后从主活动类扩展它。

此代码是异常安全,如果用户取消登录,您将能够终止到达onActivityResult的下一行代码。用户将显示登录选项,选择一个帐户进行登录。如果是RESULT_OKonActivityResult,则表示他已登录。

下一行代码将尝试异步初始化DriveClient(这里我只想要appfolder,所以尝试了getAppFolderDrive.SCOPE_APPFOLDER,你可以根据需要进行设置。)。驱动客户端初始化成功后,你会从onSuccess方法中得到DriveFolder

依赖关系

implementation('com.google.api-client:google-api-client-android:1.23.0') {
    exclude group: 'org.apache.httpcomponents'
}
implementation('com.google.apis:google-api-services-drive:v3-rev114-1.23.0') {
    exclude group: 'org.apache.httpcomponents'
}
implementation 'com.google.android.gms:play-services-auth:15.0.1'
implementation 'com.google.android.gms:play-services-drive:15.0.1'

更多帮助和文档

注意 我总是备份我的应用程序代码。如果您想查看我正在使用的整个课程,请参阅:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    • 2015-10-30
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    • 2014-06-22
    • 1970-01-01
    相关资源
    最近更新 更多