简体   繁体   English

如何检查Android设备是否具有在10.2或更高版本上运行的Google Play服务

[英]How to check the whether the android device has google play services running on 10.2 or greater

As per the documentation on official page of google the SMSRetriever api needs google play services min 10.2 on device. 根据谷歌官方页面上的文档,SMSRetriever api需要设备上的最小10.2的谷歌播放服务。

Prerequisites 先决条件

The SMS Retriever API is available only on Android devices with Play services version 10.2 and newer. SMS Retriever API仅适用于Play服务版本为10.2及更高版本的Android设备。 https://developers.google.com/identity/sms-retriever/request#2_start_the_sms_retriever https://developers.google.com/identity/sms-retriever/request#2_start_the_sms_retriever

How to implement the check for that ? 如何实施检查? I have also checked the method 我也检查过这个方法

public int isGooglePlayServicesAvailable (Context context, int minApkVersion)

of GoogleApiAvailability class. GoogleApiAvailability类。 But now again what will be value of minApkVersion in case of google play services 10.2. 但现在再次说明,在谷歌播放服务10.2的情况下,minApkVersion将具有什么价值。 Any one please let me know how to implement this in a best way. 任何人请告诉我如何以最佳方式实现这一点。

SmsRetrieverClient follows the newer GoogleApi connection pattern so you shouldn't have to worry about the version compatibility as the underlying framework handles all the resolution for you. SmsRetrieverClient遵循较新的GoogleApi连接模式,因此您不必担心版本兼容性,因为底层框架会为您处理所有分辨率。

It may actually be better UX to specifically handle the failure case as well by adding listeners to these Task-returning API calls ( https://developers.google.com/android/reference/com/google/android/gms/tasks/Task ) instead of checking for availability beforehand. 实际上,通过向这些返回任务的API调用添加侦听器来专门处理故障情况可能是更好的UX( https://developers.google.com/android/reference/com/google/android/gms/tasks/Task而不是事先检查可用性。

Just a work around to get the version comparision done. 只是努力完成版本比较。

 public boolean onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            if (googlePlayServiceEnabled() && isWithSMSRetrieverSupportedPlayServiceVersion()){
                //Good to go
                return true;
            }
            else{
                return false;
            }
    }

Checks if GooglePlay Service is Enabled and Displays proper error dialog on errors. 检查GooglePlay服务是否已启用,并在错误时显示正确的错误对话框。

private boolean googlePlayServiceEnabled() {
        try {
            GoogleApiAvailability instance = GoogleApiAvailability.getInstance();
            int responseCode = instance.isGooglePlayServicesAvailable(context);
            switch (responseCode) {
                case ConnectionResult.SUCCESS:
                    return true;
                default:
                    Dialog errorDialog = instance.getErrorDialog(this, responseCode, 123);
                    errorDialog.setCancelable(false);
                    errorDialog.show();
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
}

Implicitly gets the installed play services version and compares it with 10.2 隐式获取已安装的播放服务版本并将其与10.2进行比较

private boolean isWithSMSRetrieverSupportedPlayServiceVersion() {
        try {
            PackageInfo pi = getPackageManager().getPackageInfo(GoogleApiAvailability.GOOGLE_PLAY_SERVICES_PACKAGE, 0);
            if (pi == null)
                return false;
            if (pi.versionName == null || pi.versionName.isEmpty())
                return false;

            String playServicesVersion = pi.versionName;
            return isPlayServicesVersionValidForSMSRetriever(playServicesVersion);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

public static boolean isPlayServicesVersionValidForSMSRetriever(String v) {
        try {
            String index1 = v.substring(0, v.indexOf('.', 0));

            int versionInt1 = Integer.parseInt(index1);
            if (versionInt1 < 10)
                return false;
            else if (versionInt1 == 10) {
                String index2 = v.substring(3, 4);
                int versionInt2 = Integer.parseInt(index2);
                if (versionInt2 < 2) {
                    return false;
                } else {
                    return true;
                }
            } else
                return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

I know this is an old question, and already answered. 我知道这是一个老问题,已经回答了。 But I need to write this for the record. 但我需要写这个记录。


Situation: 情况:

In my case, galaxy S2 (google play service version 3.1.59), I couldn' get response from neither addOnSuccessListener nor addOnFailureListener . 在我的情况下,galaxy S2(谷歌播放服务版本3.1.59),我无法从addOnSuccessListeneraddOnFailureListener得到响应。 Also, addOnCompleteListener or addOnCanceledListener was not called. 此外,未调用addOnCompleteListeneraddOnCanceledListener


What I tried: 我尝试了什么:

I want use isGooglePlayServicesAvailable with minApkVersion , but there was no version history list of google play service! 我想使用isGooglePlayServicesAvailableminApkVersion ,但没有谷歌播放服务的版本历史列表!


My solution is 我的解决方案是

Use isGooglePlayServicesAvailable without minApkVersion . 使用isGooglePlayServicesAvailable而不使用minApkVersion I mean the deprecated method. 我是指弃用的方法。 In that method, it is checking google play version with GOOGLE_PLAY_SERVICES_VERSION_CODE , and this is the google play service of installed one. 在该方法中,它正在使用GOOGLE_PLAY_SERVICES_VERSION_CODE检查Google Play版本,这是已安装的Google Play服务。

@HideFirstParty
@KeepForSdk
public int isGooglePlayServicesAvailable(Context var1) {
    return this.isGooglePlayServicesAvailable(var1, GOOGLE_PLAY_SERVICES_VERSION_CODE);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM