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

Posted

技术标签:

【中文标题】如何正确检查已安装的 GooglePlayServices 是不是正常工作?【英文标题】: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

【问题讨论】:

【参考方案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 是否正常运行。

以上是关于如何正确检查已安装的 GooglePlayServices 是不是正常工作?的主要内容,如果未能解决你的问题,请参考以下文章

尝试后如何正确等待检查 Excel 实例是不是已关闭?

Laravel 5.2 或 5.3:如何正确实施检查会话是不是已登录

已解决:源 “MySQL 5.7 Community Server“ 的 GPG 密钥已安装,但是不适用于此软件包。请检查源的公钥 URL 是否配置正确 和 报错:`ERROR 1045 (28000

如何确保 Linux 驱动程序安装正确?

检查物品是不是已被处置的正确方法

如何断言显示为字符串的货币值已正确更新