如何检查Google Play服务版本?

在我的应用程序中,我需要检查Google Play服务版本(安装在用户设备中)。 可能吗 ? 如果是的话,我该怎么做? 我search了谷歌,但我找不到任何东西!

我发现简单的解决scheme

int v = getPackageManager().getPackageInfo("com.google.android.gms", 0 ).versionCode; 

如果你看看Stan0提供的链接,你会看到:

 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) 

validation在此设备上安装并启用了Google Play服务,并且此设备上安装的版本不比此客户端要求的版本旧。

返回

  • 指示是否有错误的状态码。 可以是以下ConnectionResult之一: SUCCESSSERVICE_MISSINGSERVICE_VERSION_UPDATE_REQUIREDSERVICE_DISABLEDSERVICE_INVALID

它将确保设备使用您的应用程序所需的版本,如果没有,则可以根据文档采取措施

编辑

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

这里是我的解决scheme:

 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块中写入代码作为注释,或者使用返回的布尔值来写入自定义代码。

我希望这个代码可以帮助别人。

从最新的更新,我已经结束了这个代码,我已经做了一个方便的方法来pipe理所有相关的东西..

有关服务的可用性和相关细节的所有细节都可以在这里find 。

  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;版本GoogleApiAvailability.GOOGLE_PLAY_SERVICES_VERSION_CODE; 会给你。

我在研究中发现的最有用的答案之一就是在这里。

今天早上我遇到了同样的问题。 并从stan0的build议完成。 谢谢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的相同位置。

希望它会有帮助。

如果您在应用程序pipe理器中查找Google Play服务,则会显示安装的版本。

 int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); if(status != ConnectionResult.SUCCESS) { if(status == ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED){ Toast.makeText(this,"please udpate your google play service",Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "please download the google play service", Toast.LENGTH_SHORT).show(); } } 
 final int gpsVersion = getResources() .getInteger(com.google.android.gms.R.integer.google_play_services_version); 

使用以下function来检查是否可以使用谷歌播放服务

  private boolean checkGooglePlayServicesAvailable() { final int connectionStatusCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); if (GooglePlayServicesUtil.isUserRecoverableError(connectionStatusCode)) { showGooglePlayServicesAvailabilityErrorDialog(connectionStatusCode); return false; } return true; } 
 int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE); int currentVersion = getAppVersion(context); if (registeredVersion != currentVersion) { Log.i(TAG, "App version changed."); return ""; }