简体   繁体   English

如何从android中的代码检查是否启用了Google Play服务

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

I need to check whether mobile has the play services enabled /disabled / available/not installed on it. 我需要检查手机是否已启用/禁用/可用/未安装播放服务。

I use the following code 我使用以下代码

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;
        }

Even the play services is disable status is returned as "2". 甚至播放服务处于禁用状态也返回为“ 2”。 How to over come this .? 如何克服这个问题?

Your approach is fine. 您的方法很好。 Status "2" means that Google Play Services are available but need to be updated. 状态“ 2”表示可以使用Google Play服务,但需要对其进行更新。 Check ConnectionResult class for all possible statuses: 检查ConnectionResult类的所有可能状态:

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;

I am using onActivityResult to handle this case. 我正在使用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);

    }
}

on Activity Result 活动结果

 @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;

            }
        }
    }
}

Just a note for those trying to figure out how to check if google play services are enabled, the above method 对于那些试图弄清楚如何检查是否启用了Google Play服务的人,请注意上述方法

GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

is now deprecated. 现在已弃用。 Use an instance of GoogleApiAvailability instead 改用GoogleApiAvailability的实例

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM