2

I'm writing an application using Android Studio and I want the app to check whether the user of the device has Google Play Services enabled but I have no clue on how to go about doing this, can someone help?

anishdsk
  • 91
  • 1
  • 2
  • 9
  • 1
    Possible duplicate of [Check if correct Google Play Service available: "Unfortunately application has stopped working"](https://stackoverflow.com/questions/22493465/check-if-correct-google-play-service-available-unfortunately-application-has-s) – Morrison Chang Oct 28 '18 at 04:34

1 Answers1

5

You should use this code:

private boolean checkPlayServices () {
        GoogleApiAvailability gApi = GoogleApiAvailability.getInstance();
        int resultCode = gApi.isGooglePlayServicesAvailable(this);
        if (resultCode != ConnectionResult.SUCCESS) {
            if (gApi.isUserResolvableError(resultCode)) {
                gApi.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST).show();
            } else {
                Toast.makeText(this, getResources().getString(R.string.toast_playservices_unrecoverable), Toast.LENGTH_LONG).show();
                finish();
            }
            return false;
        }
        return true;
    }