I’ll try to go a little deeper, so the answer covers the whole process related to connecting and running the Google Drive Android API (GDAA). Assuming you have registered your application in the console , your application should go through 3 barriers. All of them are related to your activity, refusal to manage another type of activity (dialogue) and getting results in callbacks. I tried to write a small, meager and medium demo that can be found here . As usual, I failed unsuccessfully, the code turned out to be nothing close to the worst and the average (more like bloated and ugly :).
I noticed that many developers stumble on authorization / authentication. It must be emphasized that the "console" must know the name of the application package and its SHA1. Your application's SHA1 is contained within your * .APK file. You have 2 versions of APK files, debugging and release version, each of which has a different SHA1. This has been discussed here .
Thus, the obstacles:
1 / Check playback services
anywhere in OnCreate (), you can put an initial check if you have Google Play Services and if your version is fine.
@Override protected void onCreate(Bundle bundle) { super.onCreate(bundle); ... if (checkPlayServices() && checkUserAccount()) { GooDrive.init(this, UT.AM.getActiveEmil()); GooDrive.connect(true); } ... }
this is "checkPlayServices ()" above and looks like this
private boolean checkPlayServices() { int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); if (status != ConnectionResult.SUCCESS) { if (GooglePlayServicesUtil.isUserRecoverableError(status)) { errorDialog(status, REQ_RECOVER); } else { finish(); } return false; } return true; }
If unsuccessful (but recoverable), the control refuses the GooPlaySvcs dialog, which ultimately results in a call to 'onActivityResult' REQ_RECOVER:
@Override protected void onActivityResult(int request, int result, Intent data) { switch (request) { case REQ_RECOVER: { mIsInAuth = false; if (result == Activity.RESULT_OK) { GooDrive.connect(true); } else if (result == RESULT_CANCELED) { finish(); } break; } } super.onActivityResult(request, result, data); }
If the recovery is successful and the user has not left, the application can cheerfully continue the connection attempt, which may (or cannot) be successful (we will see later);
2 / Get a user account.
You will need a user account (gmail), Google Drive will not talk to you without it. There are several ways to get it, you can hardcode it to "myown@gmail.com", or you can get it from the list of accounts of your device using the "Select Account" dialog box:
private boolean checkUserAccount() { String email = UT.AM.getActiveEmil(); Account accnt = UT.AM.getPrimaryAccnt(true); if (email == null) { if (accnt == null) { // multiple or no accounts available, go pick/create one accnt = UT.AM.getPrimaryAccnt(false); // pre-select primary account if present Intent it = AccountPicker.newChooseAccountIntent(accnt, null, new String[]{GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE}, true, null, null, null, null ); startActivityForResult(it, REQ_ACCPICK); return false; //--------------------->>> } else { // there only one goo account registered with the device, skip the picker UT.AM.setEmil(accnt.name); } return true; //------------------>>>> } // UNLIKELY BUT POSSIBLE, // emil OK, but the account have been removed (through settings), re-select accnt = UT.AM.getActiveAccnt(); if (accnt == null) { accnt = UT.AM.getPrimaryAccnt(false); Intent it = AccountPicker.newChooseAccountIntent(accnt, null, new String[]{GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE}, true, null, null, null, null ); startActivityForResult(it, REQ_ACCPICK); return false; //------------------>>> } return true; }
the above code gets the gmail account from the cache or the list of device accounts, taking into account the possibility that the user deleted the account in the settings when the application was launched. He works in collaboration with AM (AccountManager), which caches a previously selected account. Again, this may end up in the Account Selection dialog box, resulting in a callback (same as above):
@Override protected void onActivityResult(int request, int result, Intent data) { switch (request) { case REQ_ACCPICK: { // return from account picker if (result == Activity.RESULT_OK && data != null) { String email = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME); if (UT.AM.setEmil(email) == UT.AM.CHANGED) { GooDrive.init(this, UT.AM.getActiveEmil()); GooDrive.connect(true); } } else if (UT.AM.getActiveEmil() == null) { finish(); } break; } } super.onActivityResult(request, result, data); }
If the user has not left, the application can funfully initialize GoogleApiClient and make another connection attempt, which may (or cannot) be successful;
3 / Connection error may result in onConnectionFailed (), which may have a recovery solution
@Override public void onConnectionFailed(ConnectionResult result) { if (!mIsInAuth) { if (result.hasResolution()) { try { mIsInAuth = true; result.startResolutionForResult(this, REQ_AUTH); } catch (IntentSender.SendIntentException e) { finish(); } } else { finish(); } } }
if he has a solution, it will probably require user authorization. Basically, a user who confirms that this application may have a member with Google Drive. And again, the result is returned in the onActivityResult callback.
@Override protected void onActivityResult(int request, int result, Intent data) { switch (request) { case REQ_AUTH: { mIsInAuth = false; if (result == RESULT_OK) { GooDrive.connect(true); } else if (result == RESULT_CANCELED) { finish(); } break; } } super.onActivityResult(request, result, data); }
The code here is the same as in step 1 /, i.e. connect to success, exit failure / user CANCEL. Google Drive remembers this permission, so you usually see it only once. If you need to reset, go to the web drive, Settings> Application Management> Disconnect from disk
REFERENCE:
- Step 1: If you do not overcome the obstacle, you are DOA.
- Step 2: If you already have one account or user, and you have cached it, pass it on to initialize GooDrive without listening to the user.
- Step 3: If the user has allowed the application, he will never cheat him again, unless he / she contacts the "Disconnect from disk" on the Internet.
Steps 2 and 3 may not be performed in the order described here, it depends on your application logic.
Good luck.