Retrieve account name with the NEW Google Drive API

后端 未结 3 1354
清歌不尽
清歌不尽 2020-12-20 07:05

I set up the authorization process for Google Play Services as described under https://developers.google.com/drive/android/auth

Now that the user has authorized the

3条回答
  •  半阙折子戏
    2020-12-20 07:35

    I assume that you already have QUICKSTART or DEMO, or something similar up-and-running, so I will refer to these 2 examples. In the BaseDemoActivity.java code, you'll notice that account selection is invoked when connection fails,

    @Override public void onConnectionFailed(ConnectionResult result) {
      ...
      result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
      ...
    }
    

    ... and it comes back in onActivityResult(). I just grab the intent data and get the KEY_ACCOUNT_NAME, it is the selected email. The code below is modified from the DEMO's BaseDemoActivity.java (I mentioned above).

    @Override protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
      switch (requestCode) {
        case REQUEST_CODE_RESOLUTION:
        if ((resultCode == RESULT_OK) && (data != null) && (data.getExtras() != null ))
          // user selected account, get it
          String email = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
        else
          finish();    // user cancelled selection, an easy solution
        break;
      }
    

    Hope it helps.

提交回复
热议问题