【问题标题】:How to check the whether the android device has google play services running on 10.2 or greater如何检查 android 设备是否有运行 10.2 或更高版本的 google play 服务
【发布时间】:2018-06-12 09:38:56
【问题描述】:

根据 google 官方页面上的文档,SMSRetriever api 需要 google play services min 10.2 在设备上。

先决条件

SMS Retriever API 仅适用于具有 Play 的 Android 设备 服务版本 10.2 及更高版本。 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


    【解决方案1】:

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

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

    【讨论】:

    • 这似乎是不正确的。我的 Galaxy S2(Google Play 服务版本 3.1.59)既不响应 addOnSuccessListener 也不响应 addOnFailureListener。
    【解决方案2】:

    只是解决版本比较问题。

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

    【讨论】:

      【解决方案3】:

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


      情况:

      在我的情况下,galaxy S2(谷歌播放服务版本 3.1.59),我无法得到来自addOnSuccessListeneraddOnFailureListener 的响应。另外,addOnCompleteListeneraddOnCanceledListener 没有被调用。


      我尝试了什么:

      我想用isGooglePlayServicesAvailableminApkVersion,但是没有google play服务的版本历史列表!


      我的解决方案是

      使用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);
      }
      

      【讨论】:

        猜你喜欢
        • 2012-11-25
        • 2011-12-11
        • 2015-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-23
        • 2013-09-15
        相关资源
        最近更新 更多