首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >GooglePlayServicesUtil vs GoogleApiAvailability

GooglePlayServicesUtil vs GoogleApiAvailability
EN

Stack Overflow用户
提问于 2015-06-24 10:40:50
回答 6查看 59.4K关注 0票数 106

我正在尝试在我的Android应用程序中使用Google Play服务。正如Google文档所说,在使用Google API之前,我们需要检查它是否可用。我已经寻找了一些方法来检查它。下面是我得到的信息:

代码语言:javascript
运行
复制
private boolean checkPlayServices() {
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (resultCode != ConnectionResult.SUCCESS) {
    if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
        GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                PLAY_SERVICES_RESOLUTION_REQUEST).show();
    } else {
        Log.i(TAG, "This device is not supported.");
        finish();
    }
    return false;
}
return true;
}

但是当我去谷歌Api GooglePlayServicesUtil页面时,https://developers.google.com/android/reference/com/google/android/gms/common/GooglePlayServicesUtil

我发现所有的函数都是弃用的。例如,方法

GooglePlayServicesUtil.isGooglePlayServicesAvailable (已弃用)

谷歌推荐使用:

GoogleApiAvailability.isGooglePlayServicesAvailable.

然而,当我尝试使用GoogleApiAvailability.isGooglePlayServicesAvailable,时,我得到了错误消息:

EN

回答 6

Stack Overflow用户

发布于 2015-06-24 10:45:46

我已经找到了解决方案。在GoogleApiAvailability中,所有方法都是公共方法,而在GooglePlayServicesUtil中,所有方法都是静态公共函数。

因此,要使用GoogleApiAvailability,正确的方法是:

代码语言:javascript
运行
复制
private boolean checkPlayServices() {
    GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();
    int result = googleAPI.isGooglePlayServicesAvailable(this);
    if(result != ConnectionResult.SUCCESS) {
        if(googleAPI.isUserResolvableError(result)) {
            googleAPI.getErrorDialog(this, result,
                    PLAY_SERVICES_RESOLUTION_REQUEST).show();
        }

        return false;
    }

    return true;
}
票数 210
EN

Stack Overflow用户

发布于 2015-09-17 23:18:48

不应该再使用GooglePlayServicesUtil类了!

下面是如何使用类GoogleApiAvailability -例如,当需要GCM (或任何其他谷歌服务)时:

代码语言:javascript
运行
复制
public static final int REQUEST_GOOGLE_PLAY_SERVICES = 1972;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState == null) {
        startRegistrationService();
    }
}

private void startRegistrationService() {
    GoogleApiAvailability api = GoogleApiAvailability.getInstance();
    int code = api.isGooglePlayServicesAvailable(this);
    if (code == ConnectionResult.SUCCESS) {
        onActivityResult(REQUEST_GOOGLE_PLAY_SERVICES, Activity.RESULT_OK, null);
    } else if (api.isUserResolvableError(code) &&
        api.showErrorDialogFragment(this, code, REQUEST_GOOGLE_PLAY_SERVICES)) {
        // wait for onActivityResult call (see below)
    } else {
        Toast.makeText(this, api.getErrorString(code), Toast.LENGTH_LONG).show();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(requestCode) {
        case REQUEST_GOOGLE_PLAY_SERVICES:
            if (resultCode == Activity.RESULT_OK) {
                Intent i = new Intent(this, RegistrationService.class); 
                startService(i); // OK, init GCM
            }
            break;

        default:
            super.onActivityResult(requestCode, resultCode, data);
    }
}

更新:

REQUEST_GOOGLE_PLAY_SERVICES是可以在onActivityResult()方法中引用的具有任意名称和值的整数常量。

此外,在上面的代码中调用this.onActivityResult()也是可以的(您也可以在其他地方调用super.onActivityResult() )。

票数 65
EN

Stack Overflow用户

发布于 2016-11-01 04:04:24

您将不得不改用GoogleApiAvailability

代码语言:javascript
运行
复制
GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance(); 
int errorCode = googleApiAvailability.isGooglePlayServicesAvailable(this);

this代表context

票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31016722

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档