安卓8或更高版本:检查Google Play服务

9

这个方法一直返回0。根据开发者文档,如果设备已经获得了最新版本的 Google Play,则此方法应该返回类似于“SUCCESS”的结果。有人知道如何使用它吗?

@Override
    public void onResume() {
        super.onResume();

        GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
        System.out.println("henkie: " + GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()));
    }
4个回答

40

它返回SUCCESS。根据文档来看,该方法的返回类型为 int,返回一个状态码表示是否存在错误。可在 ConnectionResult 中选择以下之一:SUCCESS, SERVICE_MISSING, SERVICE_VERSION_UPDATE_REQUIRED, SERVICE_DISABLED, SERVICE_INVALID。

要检查返回的内容,请使用类似以下的语句:

int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
if(status == ConnectionResult.SUCCESS) {
    //Success! Do what you want
}

int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()); if(status == ConnectionResult.SUCCESS) { //成功!做你想做的事情 } - Varun Goyal
1
"isGooglePlayServicesAvailable"已被弃用。 - Saravanabalagi Ramachandran

7
请阅读文档:0代表SUCCESS
public static final int SUCCESS
The connection was successful.
Constant Value: 0 (0x00000000)

文档


4
只需要简单地说,
int statusCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
if (statusCode == ConnectionResult.SUCCESS) {
    //OK
}

3
int state = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

        if (state == ConnectionResult.SUCCESS) {
            Toast.makeText(this, "SUCCESS", Toast.LENGTH_LONG).show();
            //goAhead();
        } else {
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(state, this, -1);
            dialog.show();
        }

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