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

问题描述 投票:1回答:3

根据谷歌官方页面上的文档,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将具有什么价值。任何人请告诉我如何以最佳方式实现这一点。

android google-play-services
3个回答
2
投票

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

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


1
投票

只是努力完成版本比较。

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

0
投票

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

情况:

在我的情况下,星系S2(谷歌播放服务版本3.1.59),我无法得到addOnSuccessListeneraddOnFailureListener的回应。此外,没有调用addOnCompleteListeneraddOnCanceledListener

我尝试了什么:

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

我的解决方案是

使用没有isGooglePlayServicesAvailableminApkVersion。我是指弃用的方法。在那种方法中,它正在使用GOOGLE_PLAY_SERVICES_VERSION_CODE检查谷歌播放版本,这是已安装的谷歌播放服务。

@HideFirstParty
@KeepForSdk
public int isGooglePlayServicesAvailable(Context var1) {
    return this.isGooglePlayServicesAvailable(var1, GOOGLE_PLAY_SERVICES_VERSION_CODE);
}
© www.soinside.com 2019 - 2024. All rights reserved.