如何检查用户是否启用了 Google Play 服务

Ani*_*ish 3 java android

我正在使用 Android Studio 编写一个应用程序,我希望该应用程序检查设备的用户是否启用了 Google Play 服务,但我不知道如何执行此操作,有人可以帮忙吗?

小智 6

你应该使用这段代码:

private boolean checkPlayServices () {
        GoogleApiAvailability gApi = GoogleApiAvailability.getInstance();
        int resultCode = gApi.isGooglePlayServicesAvailable(this);
        if (resultCode != ConnectionResult.SUCCESS) {
            if (gApi.isUserResolvableError(resultCode)) {
                gApi.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST).show();
            } else {
                Toast.makeText(this, getResources().getString(R.string.toast_playservices_unrecoverable), Toast.LENGTH_LONG).show();
                finish();
            }
            return false;
        }
        return true;
    }
Run Code Online (Sandbox Code Playgroud)