我的 Google 云端硬盘中设置了多个帐户
[email protected]
[email protected]
我想通过意图用
[email protected]
打开谷歌驱动器。
我可以使用以下功能打开 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,"[email protected]")
,但没有成功。
是否可以在意向附加中发送/指定帐户?非常感谢您的帮助。
我既不熟悉
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_OK
出现 onActivityResult
,则表示他已登录。
下一行代码将尝试异步初始化
DriveClient
(这里我只想要appfolder
,所以尝试了getAppFolder
和Drive.SCOPE_APPFOLDER
,您将根据需要进行设置。)。当驱动客户端成功初始化后,您将从 DriveFolder
方法中获得 onSuccess
。
依赖关系
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'
更多帮助和文档
注意 我始终保留我的应用程序代码的备份。如果您想查看我正在使用的整个类,请参阅此:
Exploitr 的答案对我有用,但是依赖项需要一些欺骗才能工作。 gradle 依赖项将于 2024 年起使用以下内容:
implementation 'com.google.android.gms:play-services-auth:21.0.0'
implementation 'com.google.android.gms:play-services-drive:16.1.0'
您还必须在 https://console.cloud.google.com/ 上使用所需的 google 驱动器范围、匹配的包名称和 SHA-1 证书指纹创建应用程序。如果谷歌需要一天左右的时间才能识别您的应用程序,请不要犹豫。有关更多信息,您还可以阅读:https://dev.to/mesadhan/google-drive-api-with-android-4m2e