【问题标题】:How to properly check installed GooglePlayServices is working or not?如何正确检查已安装的 GooglePlayServices 是否正常工作?
【发布时间】:2021-11-01 11:51:50
【问题描述】:

在我的场景中,一些使用小米中文 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

【问题讨论】:

    标签: android google-play-services xiaomi miui mi


    【解决方案1】:

    检查一下,也许它对你有用

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

    【讨论】:

    • 感谢您的回答,从技术上讲,GooglePlayServices 在设备上可用,但无法正常工作。我想检查 PlayServices 是否正常运行。
    猜你喜欢
    • 2012-10-16
    • 2013-10-02
    • 2013-06-23
    • 1970-01-01
    • 1970-01-01
    • 2020-12-31
    • 2013-08-10
    • 1970-01-01
    • 2012-12-12
    相关资源
    最近更新 更多