Android x Google Play Services #2 Google Play Services APK がインストールされているか確認する

2013.06.18

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

Google Play Services API を利用する前に

前回は Google Play Services の概要と Android プロジェクトへの SDK の導入方法を学びました。あとはこの SDK を使って実装するわけですが、その前に Google Play Services APK というものが端末にインストールされているか確認しなければいけません。
ということで今回は「端末に Google Play Services APK がインストールされているか確認する」という実装をしてみたいと思います。
ちなみにこのアプリの日本語名は「Google Play開発者サービス」という名前で自動的にインストールされています。誤解を生みそうな名前ですが決して怪しいものではありません。。

google_play_services_available01_2

Google Play Service APK がインストールされているか確認する

それでは早速実装してみましょう。まずは実装する機能の概要について見ていきます。

  1. Google Play Services APK がインストールされているか確認する
  2. インストールされていればその後の処理 (APIを呼ぶなどの処理) を実行する
  3. インストールされていなければエラーダイアログを表示する

インストールされていない場合 Google Play Services の API を使うことができないので、エラーとして処理します。SDK にはエラーダイアログを生成する API があるのでそれを表示するようにしましょう。
ということで、まずはじめに ErrorDialogFragment を実装します。Dialog を引数にとって表示するだけのシンプルなクラスです。

ErrorDialogFragment.java

package jp.classmethod.android.sample.location;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;

public class ErrorDialogFragment extends DialogFragment {

    private Dialog mDialog;

    public ErrorDialogFragment() {
        super();
        mDialog = null;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return mDialog;
    }

    public void setDialog(Dialog dialog) {
        mDialog = dialog;
    }

}

次に Activity を実装します。
Google Play Services APK がインストールされているか調べるには GooglePlayServicesUtil.isGooglePlayServicesAvailable() メソッドを使います。このメソッドの戻り値でインストール済みかどうか判別することができます。インストールされていなかった場合は GooglePlayServicesUtil.getErrorDialog() でエラーダイアログを生成します。第一引数には GooglePlayServicesUtil.isGooglePlayServicesAvailable() の戻り値、第二引数は Context 、そして第三引数は Activity の RequestCode です。エラーダイアログは Google Play Service APK のインストールを促す表示がされているので、その手順によってインストールされた場合に onActivityResult() にインストールが完了した通知がされます。
ということで onActivityResult() を実装が必要になります。onActivityResult() には RESULT_OK だったときに API を呼び出す処理を実行するようにしておきます。
すべて実装すると以下のようになります。

MainActivity.java

package jp.classmethod.android.sample.location;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;

public class MainActivity extends FragmentActivity {

    private static final int REQUEST = 9000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Google Play Services APK がインストールされているかチェックする
        checkServiceAvailable();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (REQUEST == requestCode) {
            if (RESULT_OK == resultCode) {
                // Google Play Services 利用可能
                String msg = "Google Play Services is available.";
                ((TextView) findViewById(R.id.text_view)).setText(msg);
                
                // API 呼び出し処理を記述する
                
            }
        }
    }

    private void checkServiceAvailable() {
        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if (ConnectionResult.SUCCESS == resultCode) {
            // Google Play Services 利用可能
            String msg = "Google Play Services is available.";
            ((TextView) findViewById(R.id.text_view)).setText(msg);
        } else {
            // Google Play Services 利用不可
            String msg = "Google Play Services is not available.";
            ((TextView) findViewById(R.id.text_view)).setText(msg);

            // ErrorDialog の表示
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode, this, REQUEST);
            if (dialog != null) {
                ErrorDialogFragment frag = new ErrorDialogFragment();
                frag.setDialog(dialog);
                frag.show(getSupportFragmentManager(), "error_dialog_fragment");
            }
        }
    }
    
}

では実行してみましょう。Google Play Services APK がインストールされている場合は下図のように表示されます。

google_play_services_available02

インストールされておらず Google Play Services が利用不可の場合は下図のように表示されます。

google_play_services_available03

まとめ

第2回目は Google Play Services APK (Google Play開発者サービス) がインストールされているかチェックする機能を実装しました。Google Play Services SDK の API を呼び出したい場合は必ずこのチェックをはさむように実装にしましょう。
前置きが少し長くなりましたが、次回からさまざまな API を呼び出していきます。

参考