简体   繁体   中英

Android: testing logic for users who haven't got google play services, on an emulator

I'm trying to test out the logic for users who haven't got google play services on an emulator that I setup (without using Google APIs). As expected, attempting to signin with Google causes a "get google play services" dialog to popup, but pressing on the button in the dialog just causes an error in the logcat of:

SettingsRedirect: Can't redirect to app settings for Google Play services

I want to know if this is only because I'm using an emulator or if it could indicate a bug in my code that could affect users? (ie if there is a way of making this button work on an emulator)

Edit: code for my checking gps method:

 public static boolean checkPlayServices(Activity activity, String actionWeArePerforming) {

        Timber.i("checkPlayServices: called by %s because of %s", activity.getClass().getSimpleName(), actionWeArePerforming);

        GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
        int resultCode = googleApiAvailability.isGooglePlayServicesAvailable(activity);
        int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;

        if (resultCode != ConnectionResult.SUCCESS) {

            Timber.d("checkPlayservices: resultcode was not success");

            if (googleApiAvailability.isUserResolvableError(resultCode)) {

                Timber.d("checkPlayServices: update available - resolving");

                // "user resolvable" means Google Play is available to download the last version of Play Services APK
                // This will open Google dialog fragment displaying the proper message depending on "resultCode"
                googleApiAvailability.showErrorDialogFragment(activity, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST);
            } else {

                Timber.d("checkPlayServices: update not available");

                final ViewGroup viewGroup = (ViewGroup) ((ViewGroup) activity
                        .findViewById(android.R.id.content)).getChildAt(0);

                // Should not happen. This device does not support Play Services.
                // Let's show an ultimate warning.
                Snackbar.make(viewGroup, activity.getString(R.string.cannot_download_google_play_services), Snackbar.LENGTH_LONG);

            }

            Timber.i("checkPlayServices: returning false");
            return false;
        }

        Timber.i("checkPlayServices: returning true");
        return true;
    }

I ended doing this :

 private boolean checkPlayServices() {

    // Get the availability :
    GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
    int result = googleApiAvailability.isGooglePlayServicesAvailable(this);

    if(result != ConnectionResult.SUCCESS) {

        // Build a popup :
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.main_activity_google_play_services_problem_alert_title);
        builder.setNegativeButton(R.string.common_cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

                finish();
            }
        });

        DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

                final String appPackageName = "com.google.android.gms";
                try { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName))); }
                catch (android.content.ActivityNotFoundException e) { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName))); }

                finish();
            }
        };

        // Set the messages according to the most common errors and show the popup :
        switch(result) {

            case ConnectionResult.SERVICE_MISSING : {

                builder.setMessage(R.string.main_activity_google_play_services_missing_alert_message);
                builder.setPositiveButton(R.string.main_activity_google_play_services_missing_alert_positive_button, listener);
                builder.show();

                return false;
            }

            case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED : {

                builder.setMessage(R.string.main_activity_google_play_services_needs_update_alert_message);
                builder.setPositiveButton(R.string.main_activity_google_play_services_needs_update_alert_positive_button, listener);
                builder.show();

                return false;
            }

            default: {

                builder.setMessage(getString(R.string.main_activity_google_play_services_problem_alert_message).replaceAll("\\$1", googleApiAvailability.getErrorString(result)));
                builder.setPositiveButton(R.string.main_activity_google_play_services_problem_alert_positive_button, listener);
                builder.show();

                return false;
            }
        }
    }

    return true;
}

This way :

  • if the availability is OK, the method returns true
  • if the services are not installed / need an update / have a problem, the method opens a popup
  • if the Play Store is not installed, the user will still be able to open the link in its browser

That should be OK, I guess... It's a shame that the out of the box methods provided by Google get the job only half done :/

Of course, you can do even better by starting the Play Store activity with a result and check again in onActivityResult, but I've been lazy on this one :)

It's because of the emulator, see this issue:

https://github.com/googlesamples/google-services/issues/32

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM