GooglePlayServicesUtilとGoogleApiAvailability


102

AndroidアプリでGoogle Play Serviceを使用しようとしています。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はどこにありますか?見つかりません。
mcmillab

@mcmillab +1。8.1.0から8.4.0にアップグレードし、GooglePlayServicesUtil消えてしまいました(「マイナー」アップデートの悪い習慣のように思わGoogleApiAvailabilityれます)が、代替として使用するようには見えません。
spaaarky21 2016年

:Firebaseへの更新は、このチェックアウトするとetivy.com/...
Dawid Drozd

回答:


203

私は解決策を見つけました。では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;
}

9
PLAY_SERVICES_RESOLUTION_REQUEST
Saman Sattari、

12
private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
Ferrrmolina 2016年

7
任意の整数です。値を構成できます。
Timmmm 2016年

4
一部は9000の上に何かに値を設定することを好む
matthias_b_nz

12
このGoogle Play開発者サービスライブラリ全体の設計は完全に混乱しています。それはすべて欠陥があり、それに加えて、ドキュメントが不足しているため、追跡するのが困難です。
mr5

63

クラスGooglePlayServicesUtilはもう使用しないでください!

次に、代わりにGoogleApiAvailabilityクラスを使用する方法を示します。たとえば、GCM(またはその他のGoogleサービス)が必要な場合:

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()別の場所でも呼び出します)。


2
「GooglePlayServicesUtilクラスはもう使用されるべきではありません!」と主張するソースを指摘できますか?Google Play Services APIはとても混乱しています。
Lachezar 2015年

8
非推奨としてマークされたGooglePlayServicesUtilのすべてのメソッドに加えて、javadocの上部でGoogleApiAvailabilityクラスGOOGLE_PLAY_SERVICES_PACKAGEから定数を取得するように推奨することは、Googleが通知する方法です。クラスを使用しないでください。GooglePlayServicesUtil
Alexander Farber 2015年

3
GoogleApiAvailabilityクラスに存在しない古いバージョンのGoogle Playサービスがデバイスにある場合はどうなりますか?条件式の内部でもクラスを静的に参照すると、アプリがクラッシュしませんか?
Kevin Krumwiede 2015年

6
@Kevin Krumwiede GoogleApiAvailabilityはクライアントライブラリの一部です。つまり、コードはアプリにコンパイルされます=>心配しないでください。
WindRider 2016年

9
onActivityResult()を呼び出さないでください。別のアクティビティが結果を返すときに、外部から呼び出されることを意図しています。
Yar

10

代わりにGoogleApiAvailabilityを使用する必要があります。

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

thisを表しますcontext


9

デバイスをチェックして、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;
}

0

これをすべての場所で使用されるBaseActivityクラスの楽しみとして追加しました

    fun checkGooglePlayServices(okAction : ()-> Unit , errorAction: (msg:String, isResolved:Boolean)-> Unit){
    val apiAvailability = GoogleApiAvailability.getInstance()
    val resultCode = apiAvailability.isGooglePlayServicesAvailable(this)
    if (resultCode != ConnectionResult.SUCCESS) {
        if (apiAvailability.isUserResolvableError(resultCode)) {
            apiAvailability.getErrorDialog(
                this,
                resultCode,
                PLAY_SERVICES_RESOLUTION_REQUEST
            ).show()
             // dialoe when click on ok should let user go to install/update play serices


            errorAction("dialog is shown" , true)

        } else {
          "checkGooglePlayServices  This device is not supported.".log(mTag)
            errorAction("This device is not supported",false)
        }
    }else{
        okAction()
    }
}

companion object {
    const val PLAY_SERVICES_RESOLUTION_REQUEST = 1425
}

このように使います

    (activity as? BaseActivity)?.checkGooglePlayServices({
        // ok so start map
        initializeMap()
    },
        { msg, isResolved ->
            if (!isResolved)
                context?.show(msg)

        }
    )

または、必要に応じてカスタマイズできます。

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.