Using Google Play Services

To use Play Services, you need to create a client. That client is an instance of the GoogleApiClient class. You can find the documentation for this class (and all the other Play Services classes you will be using in these two chapters) in the Play Services reference section at developer.android.com/​reference/​gms-packages.xhtml.

To create a client, create a GoogleApiClient.Builder and configure it. At a minimum, you want to configure the instance with the specific APIs you will be using. Then call build() to create an instance.

Inside your onCreate(Bundle), create an instance of GoogleApiClient.Builder and add the Location Services API to your instance.

Listing 33.8  Creating GoogleApiClient (LocatrFragment.java)

public class LocatrFragment extends Fragment {
    private ImageView mImageView;
    private GoogleApiClient mClient;

    public static LocatrFragment newInstance() {
        return new LocatrFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);

        mClient = new GoogleApiClient.Builder(getActivity())
                .addApi(LocationServices.API)
                .build();
    }

Once you do that, you need to connect to the client. Google recommends always connecting to the client in onStart() and disconnecting in onStop(). Calling connect() on your client will change what your menu button can do, too, so call invalidateOptionsMenu() to update its visible state. (You will call it one more time later, after you are told you have been connected.)

Listing 33.9  Connecting and disconnecting (LocatrFragment.java)

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    ...
}

@Override
public void onStart() {
    super.onStart();

    getActivity().invalidateOptionsMenu();
    mClient.connect();
}

@Override
public void onStop() {
    super.onStop();

    mClient.disconnect();
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

If your client is not connected, your app will not be able to do anything. So for the next step, enable or disable the button depending on whether the client is connected.

Listing 33.10  Updating the menu button (LocatrFragment.java)

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.fragment_locatr, menu);

    MenuItem searchItem = menu.findItem(R.id.action_locate);
    searchItem.setEnabled(mClient.isConnected());
}

Then add another call to getActivity().invalidateOptionsMenu() to update your menu item when you find out that you are connected. Connection state information is passed through two callback interfaces: ConnectionCallbacks and OnConnectionFailedListener. Hook up a ConnectionCallbacks listener in onCreate(Bundle) to invalidate your toolbar when you are connected.

Listing 33.11  Listening for connection events (LocatrFragment.java)

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);

    mClient = new GoogleApiClient.Builder(getActivity())
            .addApi(LocationServices.API)
            .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                @Override
                public void onConnected(Bundle bundle) {
                    getActivity().invalidateOptionsMenu();
                }

                @Override
                public void onConnectionSuspended(int i) {

                }
            })
            .build();
}

If you are curious, you can hook up an OnConnectionFailedListener and see what it reports. But it is not necessary.

With that, your Google Play Services hookup is ready.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.235.196