1 00:00:00,720 --> 00:00:04,160 I'd like to show you something important about implicit intents. 2 00:00:04,160 --> 00:00:07,040 We always want to be careful when we imply anything in case our 3 00:00:07,040 --> 00:00:08,780 intentions are unclear. 4 00:00:08,780 --> 00:00:10,280 Remember, communication is hard. 5 00:00:11,580 --> 00:00:14,930 What I mean is, what if we didn't have a map installed? 6 00:00:14,930 --> 00:00:17,390 That probably won't happen for maps specifically, but 7 00:00:17,390 --> 00:00:20,520 it could be far more likely with custom intents which we'll see later. 8 00:00:22,200 --> 00:00:26,190 We can show this by simply disabling the maps app in the emulator. 9 00:00:26,190 --> 00:00:29,190 Let's go to the home screen and then we'll go into our settings. 10 00:00:32,410 --> 00:00:38,322 So from here will go into apps and we'll scroll down a little bit and 11 00:00:38,322 --> 00:00:41,920 we wanna click on maps and then disable. 12 00:00:43,290 --> 00:00:45,530 You get a little warning but it's okay, we wanna just disable the app. 13 00:00:46,920 --> 00:00:50,450 So now if I switch back to our app, go to MusicMachine and 14 00:00:50,450 --> 00:00:53,650 I'll click on the Download button. 15 00:00:53,650 --> 00:00:55,740 And, no, we got an error. 16 00:00:55,740 --> 00:00:57,350 Unfortunately, MusicMachine has stopped. 17 00:00:57,350 --> 00:01:00,980 Okay, let's check log cat to see what it looks like. 18 00:01:00,980 --> 00:01:04,020 So if I scroll up a little bit here, we see the error right away and 19 00:01:04,020 --> 00:01:08,190 it says no activity found to handle the intent. 20 00:01:08,190 --> 00:01:11,470 Like I said, this particular example is a little contrived. 21 00:01:11,470 --> 00:01:14,000 But this could happen for real users in different ways. 22 00:01:14,000 --> 00:01:17,690 So whenever we use an implicit intent, it's a good idea to check with Android and 23 00:01:17,690 --> 00:01:19,320 see if something exists to handle it. 24 00:01:20,410 --> 00:01:21,305 Let's get rid of log cat. 25 00:01:23,260 --> 00:01:26,410 We do this by querying something called the package manager. 26 00:01:26,410 --> 00:01:28,380 Let's add a line here before we call start activity. 27 00:01:28,380 --> 00:01:32,600 We're going to make an if statement and this is built into the intent class. 28 00:01:32,600 --> 00:01:37,391 So from our intent, we can call resolveActivity and 29 00:01:37,391 --> 00:01:40,770 then getPackageManager. 30 00:01:40,770 --> 00:01:44,230 This will return null if there are no activities in the system 31 00:01:44,230 --> 00:01:45,340 that can handle this kind of intent. 32 00:01:48,350 --> 00:01:50,030 So I've got parenthese error here but 33 00:01:50,030 --> 00:01:54,420 that's okay, we want to check if this is null and then add the other parenthese, 34 00:01:54,420 --> 00:01:57,750 and in this case we want to handle the error. 35 00:02:00,130 --> 00:02:01,670 Otherwise, we are able to resolve it. 36 00:02:01,670 --> 00:02:03,185 So we do want to call startActivity. 37 00:02:04,510 --> 00:02:07,740 We'll handle the error by simply showing a message with a snackbar. 38 00:02:07,740 --> 00:02:09,240 If you haven't seen a snackbar before, 39 00:02:09,240 --> 00:02:12,560 they're a lot like toasts except unlike toasts you can trigger an action. 40 00:02:13,772 --> 00:02:17,800 Since snackbars are newer, we need to add a support library in our build.gradle file 41 00:02:17,800 --> 00:02:19,990 to include them on older versions of Android. 42 00:02:19,990 --> 00:02:21,440 So let's open that back up. 43 00:02:21,440 --> 00:02:23,310 So it's down here in the Gradle scripts. 44 00:02:23,310 --> 00:02:27,180 Click on the one for the app module and then here, in the dependencies, 45 00:02:27,180 --> 00:02:31,670 let's add a new one, compile. 46 00:02:31,670 --> 00:02:38,210 And then we want com.android.support:design and 47 00:02:38,210 --> 00:02:40,600 then the version I have is 23.2.1. 48 00:02:40,600 --> 00:02:43,530 If you need to download or check your own version, once again you can use 49 00:02:43,530 --> 00:02:46,450 the STK manager like we did earlier to see what you should plug in there. 50 00:02:48,460 --> 00:02:50,430 Okay, so let's sync these changes with Gradle. 51 00:02:52,905 --> 00:02:57,108 And now back in main activity, I'm using get for this project so I have a warning 52 00:02:57,108 --> 00:03:01,660 about files that were added, I'm gonna say Remember, Don't Ask Again and then OK. 53 00:03:03,140 --> 00:03:05,160 Now back in main activity, 54 00:03:05,160 --> 00:03:08,545 here where we have the error handling section, we can add a snackbar. 55 00:03:08,545 --> 00:03:14,220 Snackbar then we call dot make, just like we make text with a toast. 56 00:03:15,300 --> 00:03:19,170 First parameter is the parent view where this snackbar will be displayed. 57 00:03:19,170 --> 00:03:20,845 Let's go to our layout file for main activity. 58 00:03:20,845 --> 00:03:25,163 Expand res, layout, activity_main.xml and 59 00:03:25,163 --> 00:03:29,095 the root layout is a relative layout. 60 00:03:29,095 --> 00:03:31,710 Snackbars work best in coordinator layouts but 61 00:03:31,710 --> 00:03:33,810 we can still use them in a relative layout like this. 62 00:03:33,810 --> 00:03:36,040 We just need an ID for the relative layout. 63 00:03:36,040 --> 00:03:39,235 So I'm going to add one here, at a line say, 64 00:03:39,235 --> 00:03:43,950 android:id= And then in parens @+id/. 65 00:03:43,950 --> 00:03:45,610 Let's call this rootLayout. 66 00:03:48,260 --> 00:03:52,660 So back here in Main Activity, let's declare a member variable up top. 67 00:03:54,010 --> 00:03:56,130 So here somewhere around out PlaylistAdapter, 68 00:03:56,130 --> 00:04:00,180 I'm going to say private RelativeLayout. 69 00:04:00,180 --> 00:04:02,350 And let's call it mRootLayout. 70 00:04:04,590 --> 00:04:07,340 Let's scroll down to uncreate and we can add it here. 71 00:04:08,800 --> 00:04:14,851 We'll say mRootLayout = cast to a RelativeLayout, 72 00:04:14,851 --> 00:04:19,050 findViewById R.id.rootLayout. 73 00:04:19,050 --> 00:04:20,830 Now we can reference it down in our Snackbar. 74 00:04:20,830 --> 00:04:26,680 So here where we had the air we can now pass in mRootLayout And 75 00:04:26,680 --> 00:04:28,920 then the second parameter is our message. 76 00:04:28,920 --> 00:04:34,100 Let's say, sorry, nothing found to handle this request. 77 00:04:36,630 --> 00:04:40,040 I'm going to add a comma, and then drop down to new line for the third parameter. 78 00:04:40,040 --> 00:04:41,850 Which, like a toast, is a duration. 79 00:04:41,850 --> 00:04:44,874 So let's use snackbar.lengthlong. 80 00:04:46,170 --> 00:04:50,208 And finally, again like a toast, we need to show it, so we'll call .show. 81 00:04:50,208 --> 00:04:51,150 Cool. 82 00:04:51,150 --> 00:04:53,260 All right, let's see how it looks. 83 00:04:54,370 --> 00:04:54,870 We'll run it. 84 00:05:00,011 --> 00:05:03,500 Then tap on download and there's our snackbar. 85 00:05:03,500 --> 00:05:04,620 Cool. 86 00:05:04,620 --> 00:05:08,780 As a side note, if you wanted to direct users to Google Play to download 87 00:05:08,780 --> 00:05:12,990 a map app, check the teacher's notes for more info and some sample code. 88 00:05:12,990 --> 00:05:16,020 And before we go, let's renewable Google Maps so we don't forget. 89 00:05:16,020 --> 00:05:22,460 I'll switch back to settings, click enable for maps, go back in. 90 00:05:22,460 --> 00:05:25,730 And this time if I go to download, I should be able to see the location again. 91 00:05:26,960 --> 00:05:28,370 All right, cool, it's working. 92 00:05:28,370 --> 00:05:29,110 See you in the next video.