适用于Android的Google API客户端 - 如何申请帐户?

时间:2017-12-08 21:04:43

标签: android google-api-client

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

enter image description here

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

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

之后,我开始备份过程。

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

当发生这种情况时,如何提示系统再次向他/她显示该对话框?除此之外,我已经尝试将GoogleApiClient实例设置为null并重新构建它,但这并不起作用。

1 个答案:

答案 0 :(得分:0)

我已根据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'
    
  4. 然后在我的例子中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();
                }
            }
        }
    }
    

    对我来说这很好。如果它也适合你,请告诉我。