如何检查Google Play服务的版本?

97

在我的应用程序中,我需要检查用户设备中安装的Google Play服务版本号。这可行吗?如果可以,如何实现?

12个回答

101

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

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

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

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

8
如何知道Play服务版本10.2的版本代码 - Manmohan Soni
由于API28中已经弃用versionCode,您应该使用PackageInfoCompat.getLongVersionCode - G00fY

54
如果您查看Stan0提供的链接,您将会看到这个
public static final int GOOGLE_PLAY_SERVICES_VERSION_CODE

为了与此客户端版本兼容,必须在AndroidManifest.xml文件中声明最小的Google Play服务包版本(android:versionCode)。常量值为3225000(0x003135a8)。

因此,在您的清单文件中设置了这个版本号后,调用isGooglePlayServicesAvailable(context)函数:

public static int isGooglePlayServicesAvailable (Context context)

该方法验证此设备上已安装并启用Google Play服务,并且在此设备上安装的版本不早于此客户端所需的版本。

返回值

  • 状态码,指示是否存在错误。可能是以下之一:ConnectionResult中的SUCCESSSERVICE_MISSINGSERVICE_VERSION_UPDATE_REQUIREDSERVICE_DISABLEDSERVICE_INVALID

如果设备未使用您的应用程序要求的版本,则此方法将确保其使用所需的版本。否则,您可以根据文档采取相应措施。

编辑

GooglePlayServicesUtil.isGooglePlayServicesAvailable()已过时,现在应改为使用GoogleApiAvailability.isGooglePlayServicesAvailable()


3
现在的GoogleApiAvailability是单例模式,因此您应该使用以下代码:GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(activity),来检查Google Play服务是否可用。请注意,翻译保持原意不变,且不包含解释或其他非翻译内容。 - Nativ
@Nativ 有没有办法检查 Android 设备上运行的 Google Play 服务版本是否大于 10.2? - Manmohan Soni
@ManmohanSoni 我相信有这样的人。但是我目前转向iOS开发,所以对你帮助不大。 - Nativ

15

这是我的解决方案:

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块中直接编写代码(以注释形式),要么使用此方法返回的布尔值编写自定义代码。


7

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


6

从最新的更新中,我得到了这段代码,我已经制作了一个方便的方法来管理与其相关的所有内容。

有关服务可用性和相关详细信息,请单击此处查看。

 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;将为您提供相应信息。

我在研究过程中发现最有用的答案之一在这里


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

2
int apkVersion = GoogleApiAvailability.getInstance().getApkVersion(getContext());

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

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

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

1

更新(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
            }
        }

    }

}

1

今天早上我也遇到了同样的问题。根据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的同一位置相关联。


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

我没有尝试其他答案中列出的编程方法,但在应用程序管理器中搜索对我很有帮助,谢谢! - user62171

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接