我在我的Google驱动器上设置了多个帐户
account1@gmail.com account2@gmail.com
我想打开谷歌驱动器与account2@gmail.com通过意图。我能够打开谷歌驱动器应用程序使用下面的功能。
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"),但没有工作。
是否可以在附加意图中发送/指定帐户?我们将非常感谢您的帮助。
发布于 2018-05-26 06:18:08
我既不熟悉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活动类中,然后从主活动类中扩展它。
这段代码是exception-safe,,如果用户取消了登录,您将能够终止到达onActivityResult.The的下一行代码,用户将显示登录选项,以选择一个帐户进行登录。如果这是RESULT_OK at onActivityResult的话,那就意味着他已经签约了。
下一行代码将尝试异步初始化DriveClient (这里我只想要appfolder,所以尝试了getAppFolder和Drive.SCOPE_APPFOLDER,您将根据需要设置它)。当成功初始化驱动器客户端时,您将从DriveFolder方法获得onSuccess。
Dependencies
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'进一步帮助和Docs
Note我总是备份我的应用程序代码。如果您想查看我正在使用的整个类,请查看以下内容:
https://stackoverflow.com/questions/50482038
复制相似问题