YouTube

Got a YouTube account?

New: enable viewer-created translations and captions on your YouTube channel!

English subtitles

← Coding for the Location App Contd

Get Embed Code
4 Languages

Showing Revision 3 created 05/24/2016 by Udacity Robot.

  1. So first of all, we need our activity
    to implement the various callbacks and
  2. listeners that we've
    been discussing about.
  3. So, you can see here our public
    class MainActivity already extends
  4. ActionBarActivity.
  5. We want to implement a few things.
  6. So we want it to
    implement the following.
  7. It's going to implement
    GoogleApiClient.ConnectionCallbacks,
  8. GoogleApiClient.ConnectionFailedListe-
    ner, and the location listener.
  9. Instead of you watching me typing,
    I'm just going to paste these in.
  10. I've now added these, but
    we can see we have a few errors
  11. because some of the symbols
    haven't been resolved.
  12. You just need to import the correct
    libraries to resolve them, and
  13. I've added those libraries here.
  14. We can now see that we still have an
    error, and that's because there are some
  15. methods that need to be implemented and
    we haven't implemented them yet.
  16. But we will be doing
    that in just a moment.
  17. Before we do that, there's a few
    things that I want to add to the code.
  18. First up, we just want to add a tag,
  19. which is a string,
    to the code that allows you to track
  20. the name of this application if
    we're writing it out to the log.
  21. So we do that with a private
    final string, it's called tag.
  22. And you can put whatever
    you like in there.
  23. I'm just going to put LawrenceTestApp
    and the code would look like this.
  24. Next there's a few local variables that
    we need to set up within this class.
  25. Things, for example, for tracking
    the text view that we call TxtOutput,
  26. the Google API client itself,
    and a location request object.
  27. So I'll add these now.
  28. You can see when I added them in,
  29. it detected that it needed
    to import this class.
  30. So just go ahead and say okay to that.
  31. And the class will be imported.
  32. So you can see here I have a TextView
    object that I'm calling txtOutput
  33. that will just be used to
    address the TextView on the UI.
  34. I have a GoogleApiClient that
    I call mGoogleApiClient.
  35. And I have a LocationRequest
    that I call mLocationRequest.
  36. Okay, now in our onCreate method,
    we need to set up the GoogleApiClient.
  37. And we use this with
    a GoogleApiClient builder, and
  38. we add the requisite APIs to that.
  39. Let's add this code and
    then we can talk through it.
  40. So, we go to onCreate and
    I'll add the code in here.
  41. So what it simply does is it
    creates a new builder for
  42. an API client, and it adds that we
    want to use the location services API
  43. by saying just LocationServices.API
    to the addAPI method.
  44. We want the connection callbacks on the
    Google API client to come to this class.
  45. And we want
    the OnConnectionFailedListener
  46. also to come to this class,
    and then we build it.
  47. So that's now going to give us a Google
    API client that allows us to talk to
  48. Google services.
  49. Now, if you remember,
    we had a UI element called a text view.
  50. And this will render
    the location information.
  51. Of course, we want that text
    view to be addressable in code,
  52. using this txtOutput class.
  53. So let's set up something
    to do that right now.
  54. And we do that like this.
  55. We just specify that txtOutput is found
    my looking for the ID, or .id.txtOutput.
  56. If you remember, when we specified the
    text view in the XML for the activity,
  57. we called it txtOutput,
    the same name as this.
  58. We just find that view and
    then we cast it into a text view, and
  59. now we're good to go.
  60. So just to recap,
    what did we actually do here?
  61. Well first, we created an instance of
    a Google API client using its builder.
  62. We set it to use
    the LocationServices API here, and
  63. then we told it what the callbacks for
    OnConnectionFailed were this,
  64. so we're going to be handling
    them within this class, or
  65. within this activity, and
    then we hooked up to text view to code.
  66. You've actually done a lot but
    there's still more to come.
  67. So let's have a quick quiz first.