1 00:00:00,470 --> 00:00:04,260 We're broadcasting our data to the world, but is anyone listening? 2 00:00:04,260 --> 00:00:06,390 Unfortunately, there's not an easy way for 3 00:00:06,390 --> 00:00:09,440 us to receive this data in the on update method. 4 00:00:09,440 --> 00:00:14,000 If we flip over to widget provider, it's clear that there's no way for 5 00:00:14,000 --> 00:00:17,510 us to access the intent and side on update. 6 00:00:17,510 --> 00:00:22,560 And remember, app widget providers are just specialized broadcast receivers. 7 00:00:22,560 --> 00:00:26,900 So, the first step in anything happening here is a call to on receive 8 00:00:26,900 --> 00:00:28,830 where we get an intent. 9 00:00:28,830 --> 00:00:33,040 But, again, it looks like that intent isn't being passed into on update. 10 00:00:34,050 --> 00:00:37,710 To solve this issue, we're going to need to create our own action and 11 00:00:37,710 --> 00:00:40,530 override on receive to handle it. 12 00:00:40,530 --> 00:00:44,070 Let's start by creating a new constant to hold the name of our action. 13 00:00:45,140 --> 00:00:49,830 So public static final String, and let's call it TOAST_ACTION. 14 00:00:49,830 --> 00:00:55,687 And set it equal to your package name, 15 00:00:55,687 --> 00:00:58,988 dot TOAST_ACTION. 16 00:01:01,677 --> 00:01:07,547 And now, down in me on update function, where we set the action for our intent, 17 00:01:07,547 --> 00:01:12,718 instead of using appWidgetManager.ACTION_APPWIDGET_UPDATE, 18 00:01:12,718 --> 00:01:15,470 let's just use our TOAST_ACTION. 19 00:01:15,470 --> 00:01:20,050 All that's left is overwriting on receive to handle our new action. 20 00:01:20,050 --> 00:01:23,360 Let's add a couple lines below our constants and 21 00:01:23,360 --> 00:01:26,200 then use control O to override on receive. 22 00:01:27,320 --> 00:01:30,760 Then let's add a line at the top and 23 00:01:30,760 --> 00:01:34,123 check if the intent parameter has an action of toast action. 24 00:01:34,123 --> 00:01:39,138 So if intent.getAction, 25 00:01:39,138 --> 00:01:44,160 that equals toast action. 26 00:01:44,160 --> 00:01:48,050 And if it is a toast action, let's create a new string to hold the list item, 27 00:01:49,130 --> 00:01:56,500 string list item, and let's set it equal to intent.getstringextra and 28 00:01:56,500 --> 00:01:58,750 pass in our key, key item. 29 00:02:00,000 --> 00:02:02,980 Finally, let's toast what item this is. 30 00:02:02,980 --> 00:02:06,210 And to make it easier, let's pick the create a new toast version. 31 00:02:07,420 --> 00:02:10,590 And for the second parameter, lets pass on our new list item stream. 32 00:02:12,340 --> 00:02:14,120 Now let's run the app and see what we've got. 33 00:02:17,250 --> 00:02:20,777 And again, it looks like we need to drag out new instances of our widget. 34 00:02:26,345 --> 00:02:30,630 Okay, so we've just added the toasts, and now when we click on an item, 35 00:02:30,630 --> 00:02:34,100 we see which item it is toasting down there. 36 00:02:34,100 --> 00:02:36,490 But it looks like we've lost the colors again. 37 00:02:36,490 --> 00:02:40,960 When we changed our action away from the normal update action, not only did we gain 38 00:02:40,960 --> 00:02:46,180 the ability to use our own action, but we also broke its connection to On Update. 39 00:02:46,180 --> 00:02:50,080 So we're not going to get any calls to On Update with our toast action. 40 00:02:50,080 --> 00:02:54,780 However, what we can do is just call On Update ourselves 41 00:02:54,780 --> 00:02:56,350 whenever we receive a toast action. 42 00:02:58,630 --> 00:03:01,940 Let's add a line below our toast and then call On Update. 43 00:03:01,940 --> 00:03:06,075 Then let's pass on our context and an app widget manager, 44 00:03:06,075 --> 00:03:11,080 AppWidgetManager.getInstance and pass in our context. 45 00:03:11,080 --> 00:03:15,160 And then, since we aren't really using the appWidgetIds parameter anyway, 46 00:03:15,160 --> 00:03:16,130 let's just pass it null. 47 00:03:17,320 --> 00:03:21,530 Now, if we run the app again, the background changes and 48 00:03:21,530 --> 00:03:23,150 we get a toast, great work. 49 00:03:24,280 --> 00:03:25,890 But before I let you go, 50 00:03:25,890 --> 00:03:28,420 let's quickly look at another way that we could have solved that. 51 00:03:29,480 --> 00:03:33,260 Back in the on receive function, lets delete the on update call, 52 00:03:36,320 --> 00:03:40,926 and instead let's just set the action for this intent to the update action, 53 00:03:40,926 --> 00:03:48,380 intent.setaction App widget manager.action at widget update. 54 00:03:49,520 --> 00:03:54,590 Now, when we receive an intent with our toast action, we'll toast our list item, 55 00:03:54,590 --> 00:03:59,110 and then change the intense action to action app widget update. 56 00:03:59,110 --> 00:04:01,860 Then, when we call super.onreceive, 57 00:04:01,860 --> 00:04:05,310 it will have an intent that triggers the on update method. 58 00:04:05,310 --> 00:04:11,660 And if we run the app just one more time, it still works. 59 00:04:11,660 --> 00:04:12,434 Nice. 60 00:04:14,012 --> 00:04:17,490 Widgets are certainly not the most straightforward part of Android. 61 00:04:17,490 --> 00:04:20,750 But at least now you've got some idea of why that is. 62 00:04:20,750 --> 00:04:24,320 Not to mention you've also got a pretty sweet color changing widget. 63 00:04:24,320 --> 00:04:28,460 And you understand how to add actions to not only the views in your remote views, 64 00:04:28,460 --> 00:04:31,160 but also individual list items. 65 00:04:31,160 --> 00:04:32,650 Now that you've conquered widgets, 66 00:04:32,650 --> 00:04:35,860 hopefully the rest of Android will seem a little bit easier. 67 00:04:35,860 --> 00:04:37,790 And remember, if you ever need help or 68 00:04:37,790 --> 00:04:41,570 would like to take some time to help out others, check out the community. 69 00:04:41,570 --> 00:04:42,280 Until next time.