isGooglePlayServicesAvailable()在缺少服务时返回ConnectionResult.SUCCESS

时间:2017-12-27 16:08:32

标签: android google-play-services

我已经实现了代码,用于检查播放服务是否像往常一样可用:

mGooglePlayServicesAvailable = false;
GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
int resultCode = apiAvailability.isGooglePlayServicesAvailable(this);
if (resultCode == ConnectionResult.SUCCESS) {
    mGooglePlayServicesAvailable = true;
}
else if (apiAvailability.isUserResolvableError(resultCode)) {
    apiAvailability.getErrorDialog(this, resultCode, RC_AVAILABILITY).show();
}

我的问题是,即使服务丢失,resultCode似乎也是ConnectionResult.SUCCESS。这会在许多设备上崩溃我的Android应用程序。

P.S。我可以在logcat中看到以下行,这很奇怪。

E/GooglePlayServicesUtil(12419): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.

关于可能导致此行为的原因的任何想法,或者,如果有更好的方法来检查播放服务是否可用?

非常感谢,欢呼!

1 个答案:

答案 0 :(得分:1)

经过一番研究后,我发现ConnectionResult.SUCCESS背后的原因是“Google Play服务'确实可用并且是最新的。被禁用的应用是“Google Play游戏”,所以我只需要扩展我的代码以更好地处理这种情况:

mGooglePlayGamesAvailable = false;
try {
    int status = getPackageManager().getApplicationEnabledSetting("com.google.android.play.games");
    switch(status) {
        case PackageManager.COMPONENT_ENABLED_STATE_DEFAULT:
        case PackageManager.COMPONENT_ENABLED_STATE_ENABLED:
            // check if play services are up to date
            GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
            int resultCode = apiAvailability.isGooglePlayServicesAvailable(this);
            if (resultCode == ConnectionResult.SUCCESS) {
                mGooglePlayGamesAvailable = true;
            }
            else if (apiAvailability.isUserResolvableError(resultCode)) {
                apiAvailability.getErrorDialog(this, resultCode, RC_AVAILABILITY).show();
            }
            break;
    }

} catch(IllegalArgumentException e) {}

我希望这对某人有所帮助,非常感谢您的关注和愉快的编码。

P.S。如果您认为此解决方案不够优雅,请发表评论。