Raviteja
Raviteja

Reputation: 107

How to check Google Play Services is enabled/disabled from code in android

I need to check whether mobile has the play services enabled /disabled / available/not installed on it.

I use the following code

final int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if (status != ConnectionResult.SUCCESS) {
            Log.e(TAG, GooglePlayServicesUtil.getErrorString(status));
                      return false;
        } else {
            Log.i(TAG, GooglePlayServicesUtil.getErrorString(status));
                       return true;
        }

Even the play services is disable status is returned as "2". How to over come this .?

Upvotes: 1

Views: 2729

Answers (3)

Sksamy
Sksamy

Reputation: 11

I am using onActivityResult to handle this case.

public void callMarketPlace() {
    try {
//if play services are disabled so it will return request code 1 and result code 0 in OnActivity result.
        startActivityForResult(new Intent(Intent.ACTION_VIEW,
                Uri.parse("market://details?id="+ "com.google.android.gms")), 1);
    }
    catch (android.content.ActivityNotFoundException anfe) {
        startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 1);

    }
}

on Activity Result

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
 boolean isEnable = false;

    super.onActivityResult(requestCode, resultCode, data);

   if (requestCode == 1) {
        if (resultCode == 0) {
            if (isEnable){
                isEnable = false;
            } else {
               Intent intent = new Intent(); intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                Uri uri = Uri.fromParts("package", "com.google.android.gms", null); intent.setData(uri);
                startActivityForResult(intent,1);
                isEnable = true;

            }
        }
    }
}

Upvotes: 1

SethCoast
SethCoast

Reputation: 349

Just a note for those trying to figure out how to check if google play services are enabled, the above method

GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

is now deprecated. Use an instance of GoogleApiAvailability instead

GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(getActivity());

Upvotes: 0

Singed
Singed

Reputation: 1113

Your approach is fine. Status "2" means that Google Play Services are available but need to be updated. Check ConnectionResult class for all possible statuses:

public static final int SUCCESS = 0;
public static final int SERVICE_MISSING = 1;
public static final int SERVICE_VERSION_UPDATE_REQUIRED = 2;
public static final int SERVICE_DISABLED = 3;
public static final int SIGN_IN_REQUIRED = 4;
public static final int INVALID_ACCOUNT = 5;
public static final int RESOLUTION_REQUIRED = 6;
public static final int NETWORK_ERROR = 7;
public static final int INTERNAL_ERROR = 8;
public static final int SERVICE_INVALID = 9;
public static final int DEVELOPER_ERROR = 10;
public static final int LICENSE_CHECK_FAILED = 11;
public static final int CANCELED = 13;
public static final int TIMEOUT = 14;
public static final int INTERRUPTED = 15;
public static final int API_UNAVAILABLE = 16;
public static final int SIGN_IN_FAILED = 17;

Upvotes: 2

Related Questions