如何查看Google Play服务版本?

Ara*_*ini 95 android google-play-services

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

yit*_*al9 92

我发现简单的解决方案

int v = getPackageManager().getPackageInfo(GoogleApiAvailability.GOOGLE_PLAY_SERVICES_PACKAGE, 0 ).versionCode;
Run Code Online (Sandbox Code Playgroud)

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

long v = PackageInfoCompat.getLongVersionCode(getPackageManager().getPackageInfo(GoogleApiAvailability.GOOGLE_PLAY_SERVICES_PACKAGE, 0 ));
Run Code Online (Sandbox Code Playgroud)

  • 如何知道播放服务版本10.2的版本代码 (5认同)

use*_*305 51

如果您在通过Stan0提供的链接看,你会看到这个:

public static final int GOOGLE_PLAY_SERVICES_VERSION_CODE
Run Code Online (Sandbox Code Playgroud)

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

因此,当您在清单中设置它然后调用isGooglePlayServicesAvailable(context):

public static int isGooglePlayServicesAvailable (Context context)
Run Code Online (Sandbox Code Playgroud)

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

返回

  • 状态代码,指示是否有错误.可以按照ConnectionResult之一: SUCCESS,SERVICE_MISSING,SERVICE_VERSION_UPDATE_REQUIRED, SERVICE_DISABLED,SERVICE_INVALID.

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

编辑

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

  • GoogleApiAvailability现在是一个单例,因此您应该使用:GoogleApiAvailability.getInstance()。isGooglePlayServicesAvailable(activity) (2认同)

Eng*_*r T 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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

并且您有两个选项,或者直接在else块中编写代码作为注释,或者使用返回的boolean值来编写自定义代码.

我希望这段代码可以帮助别人.


ste*_*osf 7

重要提示: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();
     }
}
Run Code Online (Sandbox Code Playgroud)


Tap*_*nHP 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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

我这样用它,

GoogleApiAvailability avail;
 int PLAY_SERVICE_STATUS = avail.isGooglePlayServicesAvailable(this);
 checkPlayService(PLAY_SERVICE_STATUS);
Run Code Online (Sandbox Code Playgroud)

对于版本GoogleApiAvailability.GOOGLE_PLAY_SERVICES_VERSION_CODE;将给你.

我在研究期间找到的最有用的答案之一就是这里.