How to properly check installed GooglePlayServices is working or not?

205 Views Asked by At

In my scenario, some user using Xiaomi Chinese MIUI ROMs device does not include PlayServices. And user installed the specific working GooglePlayServices manually from a third party Installer.

After Upgrade to the new MIUI version, that GooglePlayServices is not working anymore, but still exist in a user device.

In that case googleApiAvailability.isGooglePlayServicesAvailable(activity) still returns SUCCESS;

How can I check that installed GooglePlayServices is working properly or not?

    private fun isGooglePlayServicesAvailable(activity: Activity): Boolean {
       val googleApiAvailability = GoogleApiAvailability.getInstance()
       val status = googleApiAvailability.isGooglePlayServicesAvailable(activity)
       when (status) {
           ConnectionResult.SUCCESS -> return true
           ConnectionResult.SERVICE_DISABLED,
           ConnectionResult.SERVICE_INVALID,
           ConnectionResult.SERVICE_MISSING,
           ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED -> return false
       }
       return true
   }

Request to FusedLocationProviderClient or LocationManager

if (isGooglePlayServicesAvailable(this)) {
val task: Task<LocationSettingsResponse> =
    settingsClient.checkLocationSettings(mLocationSettingsRequest)
task.addOnSuccessListener(this) { response ->
    val states = response.locationSettingsStates
    if (states.isLocationPresent) {
        //Do something
        startFusedLocationProviderClientService()
    } else {
        Log.d(TAG, "startLocationUpdates: ${states.toString()}")
    }
}
task.addOnFailureListener(this, OnFailureListener { e ->
    when ((e as ApiException).statusCode) {
        LocationSettingsStatusCodes.RESOLUTION_REQUIRED -> {
            Log.i(TAG, "Location settings are not satisfied. Attempting to upgrade " + "location settings ")

            try {
                val rae = e as ResolvableApiException
                startIntentSenderForResult(rae.resolution.intentSender, REQUEST_CHECK_SETTINGS, null, 0, 0, 0,  null)
            } catch (sie: IntentSender.SendIntentException) {
                Log.i(TAG, "PendingIntent unable to execute request.")
            }
        }
        LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE -> {
            val errorMessage = "Location settings are inadequate, and cannot be " + "fixed here. Fix in Settings."
            Log.e(TAG, errorMessage)
        }
        else -> {
            // For some device Technically GooglePlayServices is available but not functional
            Log.e(TAG, "startLocationUpdates: err ${e.message.toString()}", e)
        }
    }
})
  task.addOnCanceledListener(this, OnCanceledListener {
      Log.d(TAG, "startLocationUpdates: OnCanceledListener")
  })

  } else {
     //Non Play Services devices
     nonPlayServicesLocationManager()
 }

After a few minutes(3 minutes) later GoogleService returns as below:

2021-09-06 14:19:39.093 18455-19014/com.company.app.uat W/FA: Tasks have been queued for a long time
2021-09-06 14:21:21.305 18455-18455/com.company.app.uat E/GmsClientSupervisor: Timeout waiting for ServiceConnection callback com.google.android.gms.measurement.START
java.lang.Exception
    at bu.handleMessage(:com.google.android.gms.dynamite_measurementdynamite@[email protected] (100400-0):3)
    at android.os.Handler.dispatchMessage(Handler.java:103)
    at android.os.Looper.loop(Looper.java:224)
    at android.app.ActivityThread.main(ActivityThread.java:7562)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
2021-09-06 14:21:21.307 18455-18455/com.company.app.uat D/FA: Service connection suspended
1

There are 1 best solutions below

1
On

Check this maybe it works for you

public boolean isGooglePlayServicesAvailable(Activity activity) {
    GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
    int status = googleApiAvailability.isGooglePlayServicesAvailable(activity);
    if(status != ConnectionResult.SUCCESS) {
        if(googleApiAvailability.isUserResolvableError(status)) {
              googleApiAvailability.getErrorDialog(activity, status, 2404).show();
        }
        return false;
    }
    return true;
}

Just Add condition

if(isGooglePlayServicesAvailable())  { 
       Log.d("TAG","Play service availbable");
}else{
       Log.d("TAG","Play service not availbable");
}