如何从 android 中的代码检查 Google Play 服务是 enabled/disabled

How to check Google Play Services is enabled/disabled from code in android

我需要检查手机是否启用/禁用了播放服务/available/not 安装了它。

我使用下面的代码

final int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if (status != ConnectionResult.SUCCESS) {
            Log.e(TAG, GooglePlayServicesUtil.getErrorString(status));
                      return false;
        } else {
            Log.i(TAG, GooglePlayServicesUtil.getErrorString(status));
                       return true;
        }

即使播放服务处于禁用状态,也返回“2”。怎么克服这个.?

你的方法很好。状态“2”表示 Google Play 服务可用但需要更新。检查 ConnectionResult class 所有可能的状态:

public static final int SUCCESS = 0;
public static final int SERVICE_MISSING = 1;
public static final int SERVICE_VERSION_UPDATE_REQUIRED = 2;
public static final int SERVICE_DISABLED = 3;
public static final int SIGN_IN_REQUIRED = 4;
public static final int INVALID_ACCOUNT = 5;
public static final int RESOLUTION_REQUIRED = 6;
public static final int NETWORK_ERROR = 7;
public static final int INTERNAL_ERROR = 8;
public static final int SERVICE_INVALID = 9;
public static final int DEVELOPER_ERROR = 10;
public static final int LICENSE_CHECK_FAILED = 11;
public static final int CANCELED = 13;
public static final int TIMEOUT = 14;
public static final int INTERRUPTED = 15;
public static final int API_UNAVAILABLE = 16;
public static final int SIGN_IN_FAILED = 17;

给那些想弄清楚如何检查 google 播放服务是否启用的人的注意事项,上面的方法

GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

现已弃用。改为使用 GoogleApiAvailability 的实例

GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(getActivity());

我正在使用 onActivityResult 来处理这种情况。

public void callMarketPlace() {
    try {
//if play services are disabled so it will return request code 1 and result code 0 in OnActivity result.
        startActivityForResult(new Intent(Intent.ACTION_VIEW,
                Uri.parse("market://details?id="+ "com.google.android.gms")), 1);
    }
    catch (android.content.ActivityNotFoundException anfe) {
        startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 1);

    }
}

在 Activity 结果

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
 boolean isEnable = false;

    super.onActivityResult(requestCode, resultCode, data);

   if (requestCode == 1) {
        if (resultCode == 0) {
            if (isEnable){
                isEnable = false;
            } else {
               Intent intent = new Intent(); intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                Uri uri = Uri.fromParts("package", "com.google.android.gms", null); intent.setData(uri);
                startActivityForResult(intent,1);
                isEnable = true;

            }
        }
    }
}