0

I am invoking the google drive api client's connect() method to start a drive communication from my android app. Now for testing purpose, I put in a breakpoint inside the onConnected method and then after the breakpoint was hit, I intentionally turned off the device wifi.

Now, in these cases, it seems, even though the google drive connection was established, and there was a loss of connectivity, the drive api, just leaves that file upload / download request abruptly.

Is there, a good way to handle / notify the user of connectivity loss, while working with drive api in android?

omkar.ghaisas
  • 235
  • 5
  • 19

1 Answers1

1

Make sure you are implementing a callback to handle the connection lost. In the documentation page for the API, I could find the following:

public class MyActivity extends FragmentActivity
    implements ConnectionCallbacks, OnConnectionFailedListener {

// Request code to use when launching the resolution activity
private static final int REQUEST_RESOLVE_ERROR = 1001;
// Unique tag for the error dialog fragment
private static final String DIALOG_ERROR = "dialog_error";
// Bool to track whether the app is already resolving an error
private boolean mResolvingError = false;

...

@Override
public void onConnectionFailed(ConnectionResult result) {
    if (mResolvingError) {
        // Already attempting to resolve an error.
        return;
    } else if (result.hasResolution()) {
        try {
            mResolvingError = true;
            result.startResolutionForResult(this, REQUEST_RESOLVE_ERROR);
        } catch (SendIntentException e) {
            // There was an error with the resolution intent. Try again.
            mGoogleApiClient.connect();
        }
    } else {
        // Show dialog using GooglePlayServicesUtil.getErrorDialog()
        showErrorDialog(result.getErrorCode());
        mResolvingError = true;
    }
}

// The rest of this code is all about building the error dialog

/* Creates a dialog for an error message */
private void showErrorDialog(int errorCode) {
    // Create a fragment for the error dialog
    ErrorDialogFragment dialogFragment = new ErrorDialogFragment();
    // Pass the error that should be displayed
    Bundle args = new Bundle();
    args.putInt(DIALOG_ERROR, errorCode);
    dialogFragment.setArguments(args);
    dialogFragment.show(getSupportFragmentManager(), "errordialog");
}

/* Called from ErrorDialogFragment when the dialog is dismissed. */
public void onDialogDismissed() {
    mResolvingError = false;
}

/* A fragment to display an error dialog */
public static class ErrorDialogFragment extends DialogFragment {
    public ErrorDialogFragment() { }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Get the error code and retrieve the appropriate dialog
        int errorCode = this.getArguments().getInt(DIALOG_ERROR);
        return GooglePlayServicesUtil.getErrorDialog(errorCode,
                this.getActivity(), REQUEST_RESOLVE_ERROR);
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        ((MainActivity)getActivity()).onDialogDismissed();
    }
}
}
Rivero
  • 911
  • 1
  • 6
  • 10
  • But would the onConnectionFailed be called, when onConnected has already fired and its time for the api to actuall call the newDriveContents or file.open or file.openContents?? Because, I was using debugger and had stopped the execution, after onConnected had been called and then I intentionallu turned off the wifi. The code crashed then and there and at other time, it didn't do anything, leaving the user to be surprised, whether the backup was sucecssful or not – omkar.ghaisas Dec 19 '14 at 19:35