トラッキング コード

3/15/2014

How to use Google Play services

Ready to use Google Play services

If you need to use Google Play services API, you should do download of Google Play services on Android SDK Manager.Google Play services was saved in following directory.<AndroidSDK>\sdk\extras\google\google_play_services\libproject\google-play-services_lib

Importing this Android Project to eclipse.

You must write a meta-data in AndroidMaifest.xml using Google Play services API.

    <application
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    </application>

Check to exist of Google Play services

You shoud Check to exist of Google Play services in a device.

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

Calling a method of GooglePlayServicesUtil.isGooglePlayServicesAvailable(), you can get Result code.If Result code is error, you can get a Dialog Object from calling method GooglePlayServicesUtil.getErrorDialog().You must use a DialogFragment to show a dialog.

If User close a dialog, onActivityResult is called with Result Code.

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CONNECTION_FAILURE_RESOLUTION_REQUEST) {
            if (resultCode == Activity.RESULT_OK) {
                // retry process
            }
        }
    }

No comments:

Post a Comment