4

I have to check if google services are active on the device. How can I check it? Looking just play services is enough?

I need this check for Huawei services.

elify
  • 440
  • 1
  • 7
  • 15

5 Answers5

2
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;
    }
2

You should use GoogleApiAvailability API. GooglePlayServicesUtil API is deprecated, don't use it.

Java:

public boolean isGooglePlayServicesAvailable(final Context context) {
    return GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS;
}

Kotlin:

fun Context.isGooglePlayServicesAvailable(): Boolean =
    GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS
m0skit0
  • 25,268
  • 11
  • 79
  • 127
1

I think this code will help you

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;
    }

.

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;
Javad Dehban
  • 1,282
  • 3
  • 11
  • 24
1

You can use these codes to check:

boolean flag = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this) == com.google.android.gms.common.ConnectionResult.SUCCESS

If flag == true, then GMS is enable in device.

If flag == false, then GMS is disable in device.

zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
1

The latest solution for 2022 is GoogleApiAvailabilityLight from package com.google.android.gms.common which is contained in dependency

com.google.android.gms:play-services-basement:18.0.0@aar

public static boolean isGooglePlayServicesAvailable(final Context context) {
  return GoogleApiAvailabilityLight.getInstance().isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS;

}

S. Gissel
  • 1,788
  • 2
  • 15
  • 32