android - Android使用指定帐户意图打开谷歌驱动器
问题描述
我在 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")
,但没有奏效。
是否可以在意图附加内容中发送/指定帐户?帮助将不胜感激。
解决方案
我既不熟悉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
活动类中,然后从Main Activity Class扩展它。
此代码是异常安全的,如果用户取消登录,您将能够终止到达下一行代码onActivityResult
。将向用户显示登录选项,选择一个帐户进行登录. 如果它出现RESULT_OK
在onActivityResult
,则表示他已登录。
下一行代码将尝试DriveClient
异步初始化(这里我想要appfolder
唯一的,所以尝试了getAppFolder
,Drive.SCOPE_APPFOLDER
你会根据需要设置它。)。成功初始化驱动客户端后,您将获得DriveFolder
fromonSuccess
方法。
依赖项
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'
进一步的帮助和文档
- https://developers.google.com/drive/api/v3/appdata
- https://developers.google.com/drive/api/v3/quickstart/android
- https://developers.google.com/drive/android/appfolder
- https://developers.google.com/drive/android/create-file
- https://www.numetriclabz.com/integrate-google-drive-in-android-tutorial/
- https://console.developers.google.com/apis/credentials/wizard?api=drive.googleapis.com
注意 我总是备份我的应用程序代码。如果您想查看我正在使用的整个课程,请参阅:
推荐阅读
- arrays - 从 EXcel VBA 中的 Join() 函数中删除分隔符
- javascript - 提示 = 背景图像 javascript
- c# - 在 C# 的协议缓冲区中找不到 csharp_outer_classname 的任何选项。有没有其他方法可以在 proto 文件中设置外部类名?
- python - 如何在 python 中的 Pi Zero 上设置 Google AIY 语音套件中声音文件的播放音量?
- python - 重复请求时出现 TwitterConnectionError
- java - Android Studio,java.lang.ClassCastException:java.lang.String 无法转换为 java.lang.Long
- angular - 使用 Docker 和 Nginx 服务 Angular 不起作用
- angular - 重定向到 Keycloak 时来自 Angular 的 CORS 错误
- flutter - 如何将抽屉放在 TabBar 的左侧?
- graphql - 图表:没有为子图查询中的 ID 参数提供值