code

GooglePlayServicesUtil 대 GoogleApiAvailability

codestyles 2020. 8. 21. 07:47
반응형

GooglePlayServicesUtil 대 GoogleApiAvailability


Android 앱에서 Google Play 서비스를 사용하려고합니다. Google 문서에 따르면 Google API를 사용하기 전에 사용할 수 있는지 확인해야합니다. 나는 그것을 확인하기 위해 어떤 방법을 찾았습니다. 내가 얻은 것은 다음과 같습니다.

private boolean checkPlayServices() {
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (resultCode != ConnectionResult.SUCCESS) {
    if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
        GooglePlayServicesUtil.getErrorDialog(resultCode, this,
                PLAY_SERVICES_RESOLUTION_REQUEST).show();
    } else {
        Log.i(TAG, "This device is not supported.");
        finish();
    }
    return false;
}
return true;
}

하지만 Google Api GooglePlayServicesUtil 페이지로 이동하면 https://developers.google.com/android/reference/com/google/android/gms/common/GooglePlayServicesUtil

모든 기능이 더 이상 사용되지 않는다는 것을 알았습니다 . 예를 들어, 방법

GooglePlayServicesUtil.isGooglePlayServicesAvailable (지원 중단됨)

그리고 Google은 다음을 사용하는 것이 좋습니다.

GoogleApiAvailability.isGooglePlayServicesAvailable .

그러나 GoogleApiAvailability.isGooglePlayServicesAvailable을 사용하려고하면 오류 메시지가 표시됩니다.

여기에 이미지 설명 입력


해결책을 찾았습니다. 에서 GoogleApiAvailability의 동안, 모든 방법이 공개되어있어서 GooglePlayServicesUtil모든 방법 정적 공용 함수이다.

따라서 GoogleApiAvailability를 사용하는 올바른 방법은 다음과 같습니다.

private boolean checkPlayServices() {
    GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();
    int result = googleAPI.isGooglePlayServicesAvailable(this);
    if(result != ConnectionResult.SUCCESS) {
        if(googleAPI.isUserResolvableError(result)) {
            googleAPI.getErrorDialog(this, result,
                    PLAY_SERVICES_RESOLUTION_REQUEST).show();
        }

        return false;
    }

    return true;
}

GooglePlayServicesUtil 클래스를 더 이상 사용하면 안됩니다!

예를 들어 GCM (또는 다른 Google 서비스)이 필요한 경우 GoogleApiAvailability 클래스를 대신 사용할 수있는 방법은 다음과 같습니다 .

public static final int REQUEST_GOOGLE_PLAY_SERVICES = 1972;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState == null) {
        startRegistrationService();
    }
}

private void startRegistrationService() {
    GoogleApiAvailability api = GoogleApiAvailability.getInstance();
    int code = api.isGooglePlayServicesAvailable(this);
    if (code == ConnectionResult.SUCCESS) {
        onActivityResult(REQUEST_GOOGLE_PLAY_SERVICES, Activity.RESULT_OK, null);
    } else if (api.isUserResolvableError(code) &&
        api.showErrorDialogFragment(this, code, REQUEST_GOOGLE_PLAY_SERVICES)) {
        // wait for onActivityResult call (see below)
    } else {
        Toast.makeText(this, api.getErrorString(code), Toast.LENGTH_LONG).show();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(requestCode) {
        case REQUEST_GOOGLE_PLAY_SERVICES:
            if (resultCode == Activity.RESULT_OK) {
                Intent i = new Intent(this, RegistrationService.class); 
                startService(i); // OK, init GCM
            }
            break;

        default:
            super.onActivityResult(requestCode, resultCode, data);
    }
}

최신 정보:

REQUEST_GOOGLE_PLAY_SERVICESonActivityResult()메서드 에서 참조 할 수있는 임의의 이름과 값을 가진 정수 상수입니다 .

또한 this.onActivityResult()위의 코드를 호출 해도 괜찮습니다 ( super.onActivityResult()다른 곳 에서도 호출 ).


대신 GoogleApiAvailability 를 사용해야 합니다.

GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance(); 
int errorCode = googleApiAvailability.isGooglePlayServicesAvailable(this);

this를 나타냅니다 context.


기기에 Google Play 서비스 APK가 있는지 확인하세요. 그렇지 않은 경우 사용자가 Google Play 스토어에서 APK를 다운로드하거나 기기의 시스템 설정에서 활성화 할 수있는 대화 상자를 표시합니다.

public static boolean checkPlayServices(Activity activity) {
    final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
    GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
    int resultCode = apiAvailability.isGooglePlayServicesAvailable(activity);
    if (resultCode != ConnectionResult.SUCCESS) {
        if (apiAvailability.isUserResolvableError(resultCode)) {
            apiAvailability.getErrorDialog(activity, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST)
                    .show();
        } else {
            Logger.logE(TAG, "This device is not supported.");
        }
        return false;
    }
    return true;
}

참고 URL : https://stackoverflow.com/questions/31016722/googleplayservicesutil-vs-googleapiavailability

반응형