【问题标题】:Google API Client for Android - how do I request an account?适用于 Android 的 Google API 客户端 - 我如何申请帐户?
【发布时间】:2017-12-09 09:25:26
【问题描述】:

在我的应用中,我为用户提供了将他/她的数据(一个数据库和一些音频文件)备份到应用文件夹中的 Google Drive 帐户的选项。为此,用户必须在以下对话框中“为 MyApp 选择帐户”:

我以这种方式创建连接后会显示该对话框:

mGAPIClient = new GoogleApiClient.Builder(mACA)
                .enableAutoManage(mACA, this)
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_APPFOLDER)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

之后,我开始备份过程。

但是,如果用户点击该对话框之外的某个位置,它就会消失并且不会建立连接 - 并且不会再次显示该对话框。

发生这种情况时,我如何提示系统再次向他/她显示该对话框?除其他外,我尝试将 GoogleApiClient 实例设置为 null 并重新构建它,但没有成功。

【问题讨论】:

  • 你检查过this answer?
  • @MatPag 不是那个,但我试过打电话给GoogleApiClient.clearDefaultAccountAndReconnect();。我相信你必须连接才能工作(这不是我的情况)。感谢您的帮助。
  • 我回家后会尽力帮助你

标签: android google-api-client


【解决方案1】:

按照this official blog post 的建议,我使用了最新的GoogleApi 客户端,并且我使用了AGP 3.0.1 和Gradle 4.2.1。

  1. 将此类路径添加到项目 build.gradle 依赖项中

    classpath 'com.google.gms:google-services:3.1.2'
    
  2. 将这些依赖项添加到 app build.gradle

    implementation 'com.google.android.gms:play-services-auth:11.6.2'
    implementation 'com.google.android.gms:play-services-drive:11.6.2'
    
  3. 在 app build.gradle 文件底部添加插件行

    apply plugin: 'com.google.gms.google-services'
    

然后在我的示例中Activity 我有:

public class MainActivity extends AppCompatActivity {

    private static final int RC_SIGN_IN = 9001;

    private GoogleSignInClient mSignInClient;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //request access to drive
        GoogleSignInOptions options =
                new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                        .requestScopes(Drive.SCOPE_FILE)
                        .build();

        mSignInClient = GoogleSignIn.getClient(this, options);
        //button to launch the signIn flow
        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(view -> signIn());
    }

    private void signIn() {
        // Launches the sign in flow, the result is returned in onActivityResult
        Intent intent = mSignInClient.getSignInIntent();
        startActivityForResult(intent, RC_SIGN_IN);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RC_SIGN_IN) {
            Task<GoogleSignInAccount> task =
                    GoogleSignIn.getSignedInAccountFromIntent(data);
            if (task.isSuccessful()) {
                // Sign in succeeded, proceed with account
                GoogleSignInAccount acct = task.getResult();
                // proceed with your logic
            } else {
                // Sign in failed, handle failure and update UI
                // we should probably show a motivation screen to the user before showing
                // the account selection again, but for example purposes we launch the
                // sign-in flow immediately
                signIn();
            }
        }
    }
}

对我来说,这很好用。让我知道它是否也适合你。

【讨论】:

  • 我希望避免这种新方法,因为我不想重写当前使用GoogleAPIClient 实现的整个备份和恢复备份过程。我会权衡我的选择......谢谢!
  • 认为新方法比旧方法好很多。它有很多好处,而且几乎在各个方面都更智能。 (也许转换并没有你想象的那么耗时);)
  • 这比我想象的还要麻烦。 GoogleSignIn...服务插件...google-services.json...DriveResourceClient...我必须从头开始。 FML。无论如何感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-02
  • 2021-04-26
相关资源
最近更新 更多