[android] Google Play Serviceのバージョンを確認してアラートを表示させる

目次

Google Serviceのバージョンを確認してアラートを表示させる

Androidアプリを制作しているとGoogle Play Serviceを利用することが多いが
特に外部SDKを利用する場合、最低利用Google Play Serviceのバージョンが指定されていることが多い。

アプリを正しく使ってもらうためにGoogle Play Serviceのバージョンを確認し、
アプリにマッチしていない場合はアラートとUpdateウィンドウを表示させる

サンプルコード:

private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(layout.activity_login);

        checkPlayServices();
    }

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;
    }

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です