繁体   English   中英

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

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

根据谷歌官方页面上的文档,SMSRetriever api需要设备上的最小10.2的谷歌播放服务。

先决条件

SMS Retriever API仅适用于Play服务版本为10.2及更高版本的Android设备。 https://developers.google.com/identity/sms-retriever/request#2_start_the_sms_retriever

如何实施检查? 我也检查过这个方法

public int isGooglePlayServicesAvailable (Context context, int minApkVersion)

GoogleApiAvailability类。 但现在再次说明,在谷歌播放服务10.2的情况下,minApkVersion将具有什么价值。 任何人请告诉我如何以最佳方式实现这一点。

SmsRetrieverClient遵循较新的GoogleApi连接模式,因此您不必担心版本兼容性,因为底层框架会为您处理所有分辨率。

实际上,通过向这些返回任务的API调用添加侦听器来专门处理故障情况可能是更好的UX( https://developers.google.com/android/reference/com/google/android/gms/tasks/Task而不是事先检查可用性。

只是努力完成版本比较。

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

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

隐式获取已安装的播放服务版本并将其与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;
    }

我知道这是一个老问题,已经回答了。 但我需要写这个记录。


情况:

在我的情况下,galaxy S2(谷歌播放服务版本3.1.59),我无法从addOnSuccessListeneraddOnFailureListener得到响应。 此外,未调用addOnCompleteListeneraddOnCanceledListener


我尝试了什么:

我想使用isGooglePlayServicesAvailableminApkVersion ,但没有谷歌播放服务的版本历史列表!


我的解决方案是

使用isGooglePlayServicesAvailable而不使用minApkVersion 我是指弃用的方法。 在该方法中,它正在使用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