Finishing Up4:43 with Ben Deitch
In this video we finish the widget!
We're broadcasting our data to the world, but is anyone listening? 0:00 Unfortunately, there's not an easy way for 0:04 us to receive this data in the on update method. 0:06 If we flip over to widget provider, it's clear that there's no way for 0:09 us to access the intent and side on update. 0:14 And remember, app widget providers are just specialized broadcast receivers. 0:17 So, the first step in anything happening here is a call to on receive 0:22 where we get an intent. 0:26 But, again, it looks like that intent isn't being passed into on update. 0:28 To solve this issue, we're going to need to create our own action and 0:34 override on receive to handle it. 0:37 Let's start by creating a new constant to hold the name of our action. 0:40 So public static final String, and let's call it TOAST_ACTION. 0:45 And set it equal to your package name, 0:49 dot TOAST_ACTION. 0:55 And now, down in me on update function, where we set the action for our intent, 1:01 instead of using appWidgetManager.ACTION_APPWIDGET_UPDATE, 1:07 let's just use our TOAST_ACTION. 1:12 All that's left is overwriting on receive to handle our new action. 1:15 Let's add a couple lines below our constants and 1:20 then use control O to override on receive. 1:23 Then let's add a line at the top and 1:27 check if the intent parameter has an action of toast action. 1:30 So if intent.getAction, 1:34 that equals toast action. 1:39 And if it is a toast action, let's create a new string to hold the list item, 1:44 string list item, and let's set it equal to intent.getstringextra and 1:49 pass in our key, key item. 1:56 Finally, let's toast what item this is. 2:00 And to make it easier, let's pick the create a new toast version. 2:02 And for the second parameter, lets pass on our new list item stream. 2:07 Now let's run the app and see what we've got. 2:12 And again, it looks like we need to drag out new instances of our widget. 2:17 Okay, so we've just added the toasts, and now when we click on an item, 2:26 we see which item it is toasting down there. 2:30 But it looks like we've lost the colors again. 2:34 When we changed our action away from the normal update action, not only did we gain 2:36 the ability to use our own action, but we also broke its connection to On Update. 2:40 So we're not going to get any calls to On Update with our toast action. 2:46 However, what we can do is just call On Update ourselves 2:50 whenever we receive a toast action. 2:54 Let's add a line below our toast and then call On Update. 2:58 Then let's pass on our context and an app widget manager, 3:01 AppWidgetManager.getInstance and pass in our context. 3:06 And then, since we aren't really using the appWidgetIds parameter anyway, 3:11 let's just pass it null. 3:15 Now, if we run the app again, the background changes and 3:17 we get a toast, great work. 3:21 But before I let you go, 3:24 let's quickly look at another way that we could have solved that. 3:25 Back in the on receive function, lets delete the on update call, 3:29 and instead let's just set the action for this intent to the update action, 3:36 intent.setaction App widget manager.action at widget update. 3:40 Now, when we receive an intent with our toast action, we'll toast our list item, 3:49 and then change the intense action to action app widget update. 3:54 Then, when we call super.onreceive, 3:59 it will have an intent that triggers the on update method. 4:01 And if we run the app just one more time, it still works. 4:05 Nice. 4:11 Widgets are certainly not the most straightforward part of Android. 4:14 But at least now you've got some idea of why that is. 4:17 Not to mention you've also got a pretty sweet color changing widget. 4:20 And you understand how to add actions to not only the views in your remote views, 4:24 but also individual list items. 4:28 Now that you've conquered widgets, 4:31 hopefully the rest of Android will seem a little bit easier. 4:32 And remember, if you ever need help or 4:35 would like to take some time to help out others, check out the community. 4:37 Until next time. 4:41
You need to sign up for Treehouse in order to download course files.Sign up