Google Play開発者サービスのバージョンを確認するにはどうすればよいですか?


95

アプリケーションで、Google Play開発者サービスのバージョン(ユーザーのデバイスにインストールされている)を確認する必要があります。出来ますか ?はいの場合、どうすればよいですか?Googleを検索しましたが何も見つかりませんでした。

回答:


94

私は簡単な解決策を見つけました:

int v = getPackageManager().getPackageInfo(GoogleApiAvailability.GOOGLE_PLAY_SERVICES_PACKAGE, 0 ).versionCode;

ただしversionCode、API 28では非推奨であるため、以下を使用できますPackageInfoCompat

long v = PackageInfoCompat.getLongVersionCode(getPackageManager().getPackageInfo(GoogleApiAvailability.GOOGLE_PLAY_SERVICES_PACKAGE, 0 ));

6
プレイサービスのバージョン10.2のバージョンコードを知る方法
Manmohan Soni

versionCodeはAPI28で非推奨になっているため、PackageInfoCompat.getLongVersionCode
G00fY

51

あなたはStan0によって提供されたリンクを見ると次のように表示されますこれを

public static final int GOOGLE_PLAY_SERVICES_VERSION_CODE

このクライアントバージョンとの互換性を保つための、Google Play開発者サービスパッケージの最小バージョン(androidManifest.xml android:versionCodeで宣言)。定数値:3225000(0x003135a8)

したがって、マニフェストでそれを設定してから、次を呼び出しますisGooglePlayServicesAvailable(context)

public static int isGooglePlayServicesAvailable (Context context)

このデバイスにGoogle Play開発者サービスがインストールされて有効になっていること、およびこのデバイスにインストールされているバージョンがこのクライアントに必要なバージョンより古いことを確認します。

戻り値

  • エラーがあったかどうかを示すステータスコード。ConnectionResultで、次のいずれかになります SUCCESSSERVICE_MISSINGSERVICE_VERSION_UPDATE_REQUIREDSERVICE_DISABLEDSERVICE_INVALID

デバイスがアプリに必要なバージョンを使用していることを確認します。使用していない場合は、ドキュメントに従ってアクションを実行できます

編集する

GooglePlayServicesUtil.isGooglePlayServicesAvailable()は非推奨であり、GoogleApiAvailability.isGooglePlayServicesAvailable()代わりに使用する必要があります。


2
GoogleApiAvailabilityがシングルトンになりましたので、次を使用してください:GoogleApiAvailability.getInstance()。isGooglePlayServicesAvailable(activity)
Nativ

@Nativには、Androidデバイスで実行されているGoogle Play開発者サービスのバージョンが10.2よりも大きいかどうかを確認する方法があります
Manmohan Soni

@ManmohanSoniあると確信しています。私は現在iOS開発にシフトしているので、あなたの助けにはなれません。
Nativ 2018年

15

これが私の解決策です:

private boolean checkGooglePlayServices() {
        final int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if (status != ConnectionResult.SUCCESS) {
            Log.e(TAG, GooglePlayServicesUtil.getErrorString(status));

            // ask user to update google play services.
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, 1);
            dialog.show();
            return false;
        } else {
            Log.i(TAG, GooglePlayServicesUtil.getErrorString(status));
            // google play services is updated. 
            //your code goes here...
            return true;
        }
    }

2つのオプションがあります。コメント付きでelseブロックにコードを直接書き込むか、このメソッドに返されたブール値を使用してカスタムコードを記述します。

このコードが誰かに役立つことを願っています。


6

新しい更新から、このコードができあがり、それに関連するすべてのものを管理する便利な方法を作りました。

サービスの可用性および関連する詳細に関するすべての詳細は、こちらから入手できます

 private void checkPlayService(int PLAY_SERVICE_STATUS)
    {
        switch (PLAY_SERVICE_STATUS)
        {
            case ConnectionResult.API_UNAVAILABLE:
                //API is not available
                break;
            case ConnectionResult.NETWORK_ERROR:
                //Network error while connection 
                break;
            case ConnectionResult.RESTRICTED_PROFILE:
                //Profile is restricted by google so can not be used for play services
                break;
            case ConnectionResult.SERVICE_MISSING:
                //service is missing
                break;
            case ConnectionResult.SIGN_IN_REQUIRED:
                //service available but user not signed in
                break;
            case ConnectionResult.SUCCESS:
                break;
        }
    }

こんな風に使っています

GoogleApiAvailability avail;
 int PLAY_SERVICE_STATUS = avail.isGooglePlayServicesAvailable(this);
 checkPlayService(PLAY_SERVICE_STATUS);

そしてバージョンについてGoogleApiAvailability.GOOGLE_PLAY_SERVICES_VERSION_CODE;はあなたに与えるでしょう。

そして、私が研究中に見つけた最も有用な答えの1つがここにあります。


6
int status = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this);
if(status != ConnectionResult.SUCCESS) {
     if(status == ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED){
        Toast.makeText(this,"please update your google play service",Toast.LENGTH_SHORT).show();
     }
     else {
        Toast.makeText(this, "please download the google play service", Toast.LENGTH_SHORT).show();
     }
}

4

重要:GoogleApiAvailability.GOOGLE_PLAY_SERVICES_VERSION_CODEは、ユーザーのデバイスにインストールされているPlay Servicesのバージョンではなく、アプリに必要な最小バージョンを返します。


1

今朝も同じ問題に遭遇しました。そして、それをstan0からのアドバイスによって成し遂げました。ありがとうstan0。

https://developers.google.com/maps/documentation/android/mapのサンプルコードに基づいて必要な変更は1つだけです。

private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the
    // map.
    if (mMap == null) {
        FragmentManager mgr = getFragmentManager();
        MapFragment mapFragment = (MapFragment)mgr.findFragmentById(R.id.map);
        mMap = mapFragment.getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            // The Map is verified. It is now safe to manipulate the map.
            setUpMap();
        }else{
            // check if google play service in the device is not available or out-dated.
            GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

            // nothing anymore, cuz android will take care of the rest (to remind user to update google play service).
        }
    }
}

さらに、manifest.xmlに次のように属性を追加する必要があります。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your.package.name"
android:versionCode="3225130"
android:versionName="your.version" >

また、「3225130」の値は、プロジェクトが使用しているgoogle-play-services_libから取得されます。また、その値はgoogle-play-services_libのmanifest.xmlと同じ場所にあります。

それがお役に立てば幸いです。


1

アプリケーションマネージャでGoogle Play開発者サービスを検索すると、インストールされているバージョンが表示されます。


他の回答に記載されている他のプログラムによるアプローチは試していませんが、アプリケーションマネージャーでの検索はうまくいきました。
Kent.Green 2016年

1

更新(2019.07.03)Kotlinバージョン:

class GooglePlayServicesUtil {

    companion object {

        private const val GOOGLE_PLAY_SERVICES_AVAILABLE_REQUEST = 9000

        fun isGooglePlayServicesWithError(activity: Activity, showDialog: Boolean): Boolean {
            val status = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(activity)
            return if (status != ConnectionResult.SUCCESS) {
                if (showDialog) {
                    GoogleApiAvailability.getInstance().getErrorDialog(activity, status, GOOGLE_PLAY_SERVICES_AVAILABLE_REQUEST).show()
                }
                true
            } else {
                false
            }
        }

    }

}

0

次の関数を使用して、Google Playサービスを使用できるかどうかを確認します

 private boolean checkGooglePlayServicesAvailable() {
    final int connectionStatusCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if (GooglePlayServicesUtil.isUserRecoverableError(connectionStatusCode)) {
        showGooglePlayServicesAvailabilityErrorDialog(connectionStatusCode);
        return false;
    }
    return true;
}

0
int apkVersion = GoogleApiAvailability.getInstance().getApkVersion(getContext());

同じ結果が得られます:

int v = getPackageManager().getPackageInfo(GoogleApiAvailability.GOOGLE_PLAY_SERVICES_PACKAGE, 0 ).versionCode;

ただし、2つ目はtry-catch内に配置する必要があります。


-4
int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
    int currentVersion = getAppVersion(context);
    if (registeredVersion != currentVersion) {
        Log.i(TAG, "App version changed.");
        return "";
    }

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