Using an Implicit Intent4:13 with Ben Jakuben
In this video we will review how to create an implicit intent that shows a user's location on a map in another app.
Welcome back, I'm really excited about this next example because it demonstrates 0:04 one of the fundamental virtues of Android that make me enjoy it. 0:08 Implicit intents, remember implicit intents allow us to easily leverage 0:12 other apps or components of Android. 0:17 This modularization with easy communication changes our device 0:19 from a grab bag of independent apps to a system of integrated components. 0:23 In the last video, I challenged you to try creating an implicit intent 0:29 about viewing a location on a map. 0:32 Were you able to get it working? 0:34 If so well done. 0:36 Either way, let's take a look at how to use implicit intents. 0:37 First either delete or 0:42 comment out this code that we use to construct an explicit intent. 0:43 I'll comment mine out so that it's here for reference. 0:46 Now let's add an implicit intent. 0:50 So we'll set this up with intent intent = new Intent. 0:53 How should we construct this intent? 1:00 What constructor do we use? 1:02 Let's take a look at some documentation. 1:03 This is the page I had in the teacher's notes before. 1:06 This lists a bunch of common intents like alarm clock, camera and maps. 1:09 Click on maps or jump to it. 1:14 So, here it tells us to show a location on a map. 1:17 We need to use the intent action called ACTION_VIEW, and 1:19 if we scroll down a little bit, we can see an example. 1:24 Okay so look at this, we want to construct an intent with an action but 1:28 no explicit class to handle that action. 1:32 We'll add some data and then we'll call start activity and 1:35 let the Android system find an appropriate activity to handle it. 1:38 So let's add Intent.ACTION_VIEW as the parameter for our constructor. 1:43 Then, for the location, we need a URI, a uniform resource identifier. 1:52 Let's create one called geolocation. 1:56 URI, with a capital U, geolocation. 1:58 And then we can pass a URI string into the Uri.parse method. 2:01 So what string do we need? 2:08 Geolocation URIs have a specific format and 2:10 we happen to have a reference in the android docs we were just looking at. 2:13 Let me scroll back up a little bit. 2:18 In here, we can see the different ways to encode a GeoUri. 2:20 We can pass in the specific latitude and longitude coordinates of a place. 2:25 But if we scroll down a little bit, we can use this one here that allows us to 2:30 provide a label. 2:33 So in here we start with geo:0,0? 2:38 And that query string q= and then let's get the latitude and longitude for 2:44 the tree house office. 2:48 We can easily get coordinates for anything from Google Maps. 2:50 On any point in Google Maps just right click on it and select what's here. 2:53 So down here we have a little bit of detail and 2:59 if you click on these coordinates, you can see it brings up more over here and 3:00 now I can copy and paste from this box. 3:04 So I'm going to copy, go back to Android Studio. 3:07 Paste it here and then in parentheses add a label Treehouse. 3:10 So now we can set the data for this intent with this URI. 3:17 Intent.setData and notice it takes a URI parameter so we can pass in geolocation. 3:20 This is a little bit different than extras. 3:29 Extras on intents are for any kind of data but 3:31 intents have a specific piece of data that must be a URI. 3:34 That's used for various things like this. 3:37 Finally, let's start an activity for this intent. 3:40 Start activity with the intent and let's run it. 3:42 Okay let's tap on the download button. 3:53 And you may get a splash screen here with the welcome to Google Maps. 3:55 So I'm going to say accept and continue. 4:00 Skip signing in, and bring it up on a map. 4:03 Got it, cool. 4:06 Look, there it is. 4:07 There's the tree house office. 4:08 I'm actually waving, but it's not showing my hand at the zoom level. 4:09
You need to sign up for Treehouse in order to download course files.Sign up