Google Cloud Messaging: how to ask for google play services availability with GoogleApiAvailability Class

179 Views Asked by At

i want to work with google cloud messaging for my android app. it is said that you need to check for google play services availability first. but i don't know how to implement this. just searched through GoogleApiAvailability Class. Problem is i don't understand how to set up check and use method .getErrorDialog() in my case. It is said requestCode is "The requestCode given when calling startActivityForResult". but how to use it?

public void onResume() {
    GoogleApiAvailability availability = GoogleApiAvailability.getInstance();

    int checkForGPS = availability.isGooglePlayServicesAvailable(this);
    if(checkForGPS != ConnectionResult.SUCCESS) {
        availability.getErrorDialog(this, checkForGPS, requestCode);
    }
    super.onResume();
}
1

There are 1 best solutions below

2
On BEST ANSWER

Try this,

private boolean checkPlayServices() {
    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if (resultCode != ConnectionResult.SUCCESS) {
        if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
            GooglePlayServicesUtil.getErrorDialog(resultCode, this, PLAY_SERVICES_RESOLUTION_REQUEST).show();
        } else {
            Log.e(TAG, "This device is not supported.");
            finish();
        }
        return false;
    }
    return true;
}