首页 > 解决方案 > 如何正确检查已安装的 GooglePlayServices 是否正常工作?

问题描述

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

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

在那种情况下googleApiAvailability.isGooglePlayServicesAvailable(activity)仍然返回 SUCCESS;

如何检查已安装的 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

标签: androidgoogle-play-servicesxiaomimiuimi

解决方案


检查这个也许它适合你

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

只需添加条件

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

推荐阅读