【问题标题】:How to check Google Play services version?如何查看 Google Play 服务版本?
【发布时间】:2013-09-15 06:25:31
【问题描述】:

在我的应用程序中,我需要检查 Google Play 服务版本(安装在用户设备中)。可能吗 ?如果是,我该怎么做?

【问题讨论】:

    标签: android google-play-services


    【解决方案1】:

    我找到了简单的解决方案:

    int v = getPackageManager().getPackageInfo(GoogleApiAvailability.GOOGLE_PLAY_SERVICES_PACKAGE, 0 ).versionCode;
    

    versionCode 在 API 28 中已弃用,因此您可以使用 PackageInfoCompat

    long v = PackageInfoCompat.getLongVersionCode(getPackageManager().getPackageInfo(GoogleApiAvailability.GOOGLE_PLAY_SERVICES_PACKAGE, 0 ));
    

    【讨论】:

    【解决方案2】:

    如果您查看 Stan0 提供的链接,您将看到 this:

    public static final int GOOGLE_PLAY_SERVICES_VERSION_CODE
    

    最低 Google Play 服务包版本 (在 AndroidManifest.xml android:versionCode 中声明) 为了与此客户端版本兼容。 常数值:3225000 (0x003135a8)

    所以,当你在清单中设置它然后调用isGooglePlayServicesAvailable(context)

    public static int isGooglePlayServicesAvailable (Context context)
    

    验证此设备上是否安装并启用了 Google Play 服务, 并且此设备上安装的版本不低于所需版本 由该客户提供。

    退货

    • 指示是否存在错误的状态代码。 可以是 ConnectionResult 中的以下之一: SUCCESS, SERVICE_MISSING, SERVICE_VERSION_UPDATE_REQUIRED, SERVICE_DISABLEDSERVICE_INVALID

    它将确保设备正在使用您的应用所需的版本,如果不是,那么您可以根据文档采取措施

    编辑

    GooglePlayServicesUtil.isGooglePlayServicesAvailable() 已弃用,现在应使用GoogleApiAvailability.isGooglePlayServicesAvailable()

    【讨论】:

    • GoogleApiAvailability 现在是单例,因此您应该使用:GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(activity)
    • @Nativ 有什么方法可以检查安卓设备上运行的 google play services 版本是否大于 10.2
    • @ManmohanSoni 我确定有。我目前正在转向 iOS 开发,所以我对您的帮助不大。
    【解决方案3】:

    这是我的解决方案:

    private boolean checkGooglePlayServices() {
            final int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
            if (status != ConnectionResult.SUCCESS) {
                Log.e(TAG, GooglePlayServicesUtil.getErrorString(status));
    
                // ask user to update google play services.
                Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, 1);
                dialog.show();
                return false;
            } else {
                Log.i(TAG, GooglePlayServicesUtil.getErrorString(status));
                // google play services is updated. 
                //your code goes here...
                return true;
            }
        }
    

    您有两个选择,或者直接在 else 块中编写代码,或者使用此方法返回的布尔值来编写自定义代码。

    【讨论】:

      【解决方案4】:

      重要提示:GoogleApiAvailability.GOOGLE_PLAY_SERVICES_VERSION_CODE 返回您的应用所需的最低版本,而不是用户设备上安装的播放服务版本。

      【讨论】:

        【解决方案5】:

        从我得到此代码的较新更新中,我制作了一种方便的方法来管理与它相关的所有内容..

        有关服务可用性的所有详细信息和相关详细信息都可以在here 获得。

         private void checkPlayService(int PLAY_SERVICE_STATUS)
            {
                switch (PLAY_SERVICE_STATUS)
                {
                    case ConnectionResult.API_UNAVAILABLE:
                        //API is not available
                        break;
                    case ConnectionResult.NETWORK_ERROR:
                        //Network error while connection 
                        break;
                    case ConnectionResult.RESTRICTED_PROFILE:
                        //Profile is restricted by google so can not be used for play services
                        break;
                    case ConnectionResult.SERVICE_MISSING:
                        //service is missing
                        break;
                    case ConnectionResult.SIGN_IN_REQUIRED:
                        //service available but user not signed in
                        break;
                    case ConnectionResult.SUCCESS:
                        break;
                }
            }
        

        我就是这样用的,

        GoogleApiAvailability avail;
         int PLAY_SERVICE_STATUS = avail.isGooglePlayServicesAvailable(this);
         checkPlayService(PLAY_SERVICE_STATUS);
        

        对于版本GoogleApiAvailability.GOOGLE_PLAY_SERVICES_VERSION_CODE; 会给你。

        我在研究中发现的最有用的答案之一is here.

        【讨论】:

          【解决方案6】:
          int status = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this);
          if(status != ConnectionResult.SUCCESS) {
               if(status == ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED){
                  Toast.makeText(this,"please update your google play service",Toast.LENGTH_SHORT).show();
               }
               else {
                  Toast.makeText(this, "please download the google play service", Toast.LENGTH_SHORT).show();
               }
          }
          

          【讨论】:

            【解决方案7】:

            如果您在应用程序管理器中查找 Google Play 服务,它将显示已安装的版本。

            【讨论】:

            • 我没有尝试过其他答案中列出的其他编程方法,但在应用程序管理器中搜索对我有用,谢谢!
            【解决方案8】:
            int apkVersion = GoogleApiAvailability.getInstance().getApkVersion(getContext());
            

            将得到与以下相同的结果:

            int v = getPackageManager().getPackageInfo(GoogleApiAvailability.GOOGLE_PLAY_SERVICES_PACKAGE, 0 ).versionCode;
            

            但第二个需要放在 try-catch 中。

            【讨论】:

              【解决方案9】:

              更新 (2019.07.03) Kotlin 版本:

              class GooglePlayServicesUtil {
              
                  companion object {
              
                      private const val GOOGLE_PLAY_SERVICES_AVAILABLE_REQUEST = 9000
              
                      fun isGooglePlayServicesWithError(activity: Activity, showDialog: Boolean): Boolean {
                          val status = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(activity)
                          return if (status != ConnectionResult.SUCCESS) {
                              if (showDialog) {
                                  GoogleApiAvailability.getInstance().getErrorDialog(activity, status, GOOGLE_PLAY_SERVICES_AVAILABLE_REQUEST).show()
                              }
                              true
                          } else {
                              false
                          }
                      }
              
                  }
              
              }
              

              【讨论】:

                【解决方案10】:

                今天早上我也遇到了同样的问题。并按照stan0的建议完成了它。谢谢 stan0。

                根据https://developers.google.com/maps/documentation/android/map 中的示例代码,只需进行一项更改。

                private void setUpMapIfNeeded() {
                    // Do a null check to confirm that we have not already instantiated the
                    // map.
                    if (mMap == null) {
                        FragmentManager mgr = getFragmentManager();
                        MapFragment mapFragment = (MapFragment)mgr.findFragmentById(R.id.map);
                        mMap = mapFragment.getMap();
                        // Check if we were successful in obtaining the map.
                        if (mMap != null) {
                            // The Map is verified. It is now safe to manipulate the map.
                            setUpMap();
                        }else{
                            // check if google play service in the device is not available or out-dated.
                            GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
                
                            // nothing anymore, cuz android will take care of the rest (to remind user to update google play service).
                        }
                    }
                }
                

                此外,您需要在 manifest.xml 中添加一个属性:

                <manifest xmlns:android="http://schemas.android.com/apk/res/android"
                package="your.package.name"
                android:versionCode="3225130"
                android:versionName="your.version" >
                

                “3225130”的值取自您的项目正在使用的 google-play-services_lib。此外,该值位于 google-play-services_lib 中 manifest.xml 的同一位置。

                【讨论】:

                  【解决方案11】:

                  使用以下函数检查是否可以使用google play服务

                   private boolean checkGooglePlayServicesAvailable() {
                      final int connectionStatusCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
                      if (GooglePlayServicesUtil.isUserRecoverableError(connectionStatusCode)) {
                          showGooglePlayServicesAvailabilityErrorDialog(connectionStatusCode);
                          return false;
                      }
                      return true;
                  }
                  

                  【讨论】:

                    【解决方案12】:
                    int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
                        int currentVersion = getAppVersion(context);
                        if (registeredVersion != currentVersion) {
                            Log.i(TAG, "App version changed.");
                            return "";
                        }
                    

                    【讨论】:

                    • 这不是解决方案。
                    猜你喜欢
                    • 2016-09-29
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2012-11-25
                    • 1970-01-01
                    • 2017-12-25
                    • 2017-04-18
                    相关资源
                    最近更新 更多