选择Google帐户时,Google Calendar API在API 19(4.4)中返回requestCode = 0

时间:2018-09-12 14:09:22

标签: java android api calendar

在api 23中选择Google帐户时,Android中的Google Calendar Api在API 19(4.4)中返回requestCode = 0。我的onActivityResult的代码是:

@Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    switch(requestCode) {
        case REQUEST_GOOGLE_PLAY_SERVICES:
            if (resultCode != RESULT_OK) {
                Helper.createCustomDialogAlert(activity,getResources().getString(R.string.not_play_services),null);
            } else {
                getResultsFromApi(mDate);
            }
            break;
        case REQUEST_ACCOUNT_PICKER:
            if (resultCode == RESULT_OK && data != null &&  //return requestCode=0 on Choose Account
                    data.getExtras() != null) {
                String accountName =
                        data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
                if (accountName != null) {
                    SharedPreferences settings =
                            activity.getPreferences(Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putString(PREF_ACCOUNT_NAME, accountName);
                    editor.apply();
                    mCredential.setSelectedAccountName(accountName);
                    getResultsFromApi(mDate);
                }
            }
            break;
        case REQUEST_AUTHORIZATION:
            if (resultCode == RESULT_OK) {
                getResultsFromApi(mDate);
            }
            break;
        default:
            super.onActivityResult(requestCode, resultCode, data);
            break;
    }
}

private void getResultsFromApi(Date eventDate) {
    if (! isGooglePlayServicesAvailable()) {
        acquireGooglePlayServices();
    } else if (mCredential.getSelectedAccountName() == null) {
        chooseAccount();
    } else if (! isDeviceOnline()) {
        Helper.showMessageAlert(activity.getResources().getString(R.string.notinternet),
        activity.getResources().getString(R.string.warning),activity,null);
    } else {
        new MakeRequestTask(mCredential,eventDate).execute();
    }
}

@AfterPermissionGranted(REQUEST_PERMISSION_GET_ACCOUNTS)
private void chooseAccount() {
    if (EasyPermissions.hasPermissions(
            activity, Manifest.permission.GET_ACCOUNTS)) {
        String accountName = activity.getPreferences(Context.MODE_PRIVATE)
                .getString(PREF_ACCOUNT_NAME, null);
        if (accountName != null) {
            mCredential.setSelectedAccountName(accountName);
            getResultsFromApi(mDate);
        } else {
            // Start a dialog from which the user can choose an account
            startActivityForResult(
                    mCredential.newChooseAccountIntent(),
                    REQUEST_ACCOUNT_PICKER);
        }
    } else {
        // Request the GET_ACCOUNTS permission via a user dialog
        EasyPermissions.requestPermissions(
                this,
                "This app needs to access your Google account (via Contacts).",
                REQUEST_PERMISSION_GET_ACCOUNTS,
                Manifest.permission.GET_ACCOUNTS);
    }
}

private void acquireGooglePlayServices() {
    GoogleApiAvailability apiAvailability =
            GoogleApiAvailability.getInstance();
    final int connectionStatusCode =
            apiAvailability.isGooglePlayServicesAvailable(activity);
    if (apiAvailability.isUserResolvableError(connectionStatusCode)) {
        showGooglePlayServicesAvailabilityErrorDialog(connectionStatusCode);
    }
}

void showGooglePlayServicesAvailabilityErrorDialog(
        final int connectionStatusCode) {
    GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
    Dialog dialog = apiAvailability.getErrorDialog(
            activity,
            connectionStatusCode,
            REQUEST_GOOGLE_PLAY_SERVICES);
    dialog.show();
}

一切都可以在API 23(Android 6)中正常运行。我一直在询问,看来Google Calendar API应该可以从Android API 11正常工作。

0 个答案:

没有答案