Google Play servicesを使う準備
Google Play services APIを使うには、Android SDK ManagerでGoogle Play servicesをダウンロードします。ダウンロードすると、次のディレクトリに保存されます。<AndroidSDK>\sdk\extras\google\google_play_services\libproject\google-play-services_lib
上記のプロジェクトをEclipseにインポートします。
Google Play services APIを使用するプロジェクトにて、AndroidManifest.xmlにmeta-dataの記載を行います。
<application <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> </application>
Google Play servicesの存在確認
アプリケーションが動作するユーザー端末がどのような状態かわかりません。Google Play services APIを使用する前に、Google Play services APKがインストールされてるかどうかチェックしましょう。
public class UsingGooglePlayServiceSampleActivity extends FragmentActivity { private boolean isGooglePlayServicesAvailable() { int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); if (ConnectionResult.SUCCESS == resultCode) { return true; } else { Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode, this, CONNECTION_FAILURE_RESOLUTION_REQUEST); if (dialog != null) { ErrorDialogFragment errorFragment = new ErrorDialogFragment(); errorFragment.setDialog(dialog); errorFragment.show(getSupportFragmentManager(), "errro_dialog"); } } return false; } public static class ErrorDialogFragment extends DialogFragment { private Dialog mDialog; public ErrorDialogFragment() { super(); mDialog = null; } public void setDialog(Dialog dialog) { mDialog = dialog; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { return mDialog; } @Override public void onDestroy() { mDialog = null; super.onDestroy(); } } }
GooglePlayServicesUtil.isGooglePlayServicesAvailable()をコールします。APIリファレンスに記載されているResultコード一覧の値が返ってきます。エラーが発生した場合、GooglePlayServicesUtil.getErrorDialog()をコールしDialogを取得します。取得したDialogはDialogFragmentを使って、表示してください。
Dialogを通じてユーザーに発生した問題を通知します。Dialogを閉じると、onActivityResultがコールされてResultを通知します。
protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == CONNECTION_FAILURE_RESOLUTION_REQUEST) { if (resultCode == Activity.RESULT_OK) { // retry process } } }
0 件のコメント:
コメントを投稿