检测设备是否支持Google Play服务

在使用Google Play服务之前,需要判断当前的设备是否支持Google Play服务,Google官方提供了两种方法来进行判断

一、使用GoogleAPiClient类访问Google Play服务功能(被建议使用)

在使用Google Play服务的地方实现 GoogleApiClient.OnConnectionFailedListener,实现回调方法onConnectionFailed( )。如果连接失败,则可以通过connectionResult.getErrorCode()获取错误代码。

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.e(TAG, "error code: " + connectionResult.getErrorCode());
}

导致Google Play服务无法使用的 code值有:SERVICE_MISSING(1),SERVICE_VERSION_UPDATE_REQUIRED(2),SERVICE_DISABLED(3)。

注意:部分机型Google Play服务不可用时,返回code值不仅为上述三种,在判断时需要特别注意。

二、使用方法isGooglePlayServicesAvailable()

使用GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(),不要使用过时的方法GooglePlayServicesUtil.isGooglePlayServicesAvailable()。

错误代码值和注意事项与方法一中一致。

PS:
出现Google Play服务不可用的错误时,可以使用GoogleApiAvailability.getInstance().getErrorDialog()弹出官方提供的对话框,这会根据不同的错误代码来给用户提供不同的操作,比如是进行升级操作还是下载操作。

你可能感兴趣的:(Android经验)