在我的场景中,一些使用小米中文MIUI ROM设备的用户不包括PlayServices。并且用户从第三方安装程序手动安装特定的工作GooglePlayServices。

升级到新的MIUI版本后,该GooglePlayServices不再工作,但仍然存在于用户设备中。

googleApiAvailability.isGooglePlayServicesAvailable(activity)

如何检查已安装的GooglePlayServices是否工作正常?

    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
   }

对FusedLocationProviderClient或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()
 }

几分钟(3分钟)后,GoogleService返回如下所示:

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@213016065@21.30.16 (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