1 00:00:00,280 --> 00:00:04,220 We've just finished setting up our RemoteViews factory, which remember, 2 00:00:04,220 --> 00:00:06,840 is just an adapter for remote use. 3 00:00:06,840 --> 00:00:09,970 That's also why we decided to call it WidgetAdapter. 4 00:00:09,970 --> 00:00:13,310 But now we need to hook up our WidgetAdapter to our widget. 5 00:00:13,310 --> 00:00:16,790 And since both of these things are dealing with RemoteViews that will be living 6 00:00:16,790 --> 00:00:21,950 inside another application, Android's got a special way that we need to handle this. 7 00:00:21,950 --> 00:00:26,360 Basically, we need to get an instance of our WidgetAdapter from within someone 8 00:00:26,360 --> 00:00:30,330 else's application, which really complicates things. 9 00:00:30,330 --> 00:00:35,000 I mean, sure, the other application could just instantiate our widget after but 10 00:00:35,000 --> 00:00:37,830 then it would be created with the wrong context. 11 00:00:37,830 --> 00:00:40,220 So we wouldn't be able to do much with it. 12 00:00:40,220 --> 00:00:41,280 To solve this problem, 13 00:00:41,280 --> 00:00:45,500 we're going to create a service that can return a new WidgetAdapter. 14 00:00:45,500 --> 00:00:49,530 Then, instead of directly setting our WidgetAdapter on our widget, 15 00:00:49,530 --> 00:00:53,000 we can just use an intent to point to our service. 16 00:00:53,000 --> 00:00:58,210 This way, our widget can get its adapter even from within another application. 17 00:00:58,210 --> 00:01:01,240 It will just use that intent to start up our service and 18 00:01:01,240 --> 00:01:02,275 retrieve that WidgetAdapter. 19 00:01:03,560 --> 00:01:05,890 Back in the code, let's create a new class for 20 00:01:05,890 --> 00:01:08,065 our service that will return a WidgetAdapter. 21 00:01:09,090 --> 00:01:11,065 And let's name it WidgetService. 22 00:01:12,520 --> 00:01:16,100 Then, Android's gone ahead and created a specialized service just for 23 00:01:16,100 --> 00:01:17,310 this occasion. 24 00:01:17,310 --> 00:01:22,777 So let's just extend the RemoteViewsService class. 25 00:01:22,777 --> 00:01:26,551 And then use Alt+Enter to implement the one required method. 26 00:01:26,551 --> 00:01:31,230 And now all we need to do is return a new instance of our WidgetAdapter. 27 00:01:31,230 --> 00:01:36,667 So instead of returning null, let's return new WidgetAdapter. 28 00:01:36,667 --> 00:01:39,971 And for the context, we can just pass in this. 29 00:01:39,971 --> 00:01:42,191 Last but not least, since this is a service, 30 00:01:42,191 --> 00:01:45,690 we're going to need to declare it in our manifest. 31 00:01:45,690 --> 00:01:49,620 Let's add some space below our last receiver tag, and 32 00:01:49,620 --> 00:01:52,276 then add a new entry for our service. 33 00:01:52,276 --> 00:01:54,830 And accept WidgetService for the name. 34 00:01:54,830 --> 00:02:01,256 Next, to prevent other apps from being able to access our widget's data, 35 00:02:01,256 --> 00:02:05,846 we need to add the android:permission attribute and 36 00:02:05,846 --> 00:02:11,559 set it equal to "android.permission.BIND_REMOTEVIEWS", 37 00:02:11,559 --> 00:02:16,280 which is also below in the teacher's notes. 38 00:02:16,280 --> 00:02:18,550 Then let's close our service tag. 39 00:02:18,550 --> 00:02:21,298 And now we're finally ready to add our adapter to our widget. 40 00:02:21,298 --> 00:02:23,885 Over in WidgetProvider let's 41 00:02:23,885 --> 00:02:28,864 add a few lines below where we initialize our RemoteViews. 42 00:02:28,864 --> 00:02:31,500 And then let's create a new intent variable pointing to our service. 43 00:02:32,600 --> 00:02:40,154 Intent, and we'll call it serviceIntent = New intent. 44 00:02:40,154 --> 00:02:42,099 And pass in a context. 45 00:02:42,099 --> 00:02:49,170 And for the class, we wanna pass in our service class, Widgetservice.class. 46 00:02:49,170 --> 00:02:54,093 On the next line, let's add our adapter to our list view by 47 00:02:54,093 --> 00:02:57,823 typing remoteViews.setRemoteAdapter. 48 00:02:57,823 --> 00:03:04,611 And then for the viewId, let's pass in the idea of our list view R.id.listView. 49 00:03:04,611 --> 00:03:07,570 And then let's pass along our serviceIntent as well. 50 00:03:07,570 --> 00:03:12,680 And now, if we run the app again, we can see that we've got our list. 51 00:03:12,680 --> 00:03:15,118 Though, let me make these a little bigger, so they are easier to see. 52 00:03:23,747 --> 00:03:28,240 And if we click on them, we can click on the buttons and scroll them up and down. 53 00:03:30,100 --> 00:03:32,350 But it looks like we've lost the ability to change color. 54 00:03:33,480 --> 00:03:36,730 In the next video, we'll find out where the color went and 55 00:03:36,730 --> 00:03:38,720 learn about how we can interact with our list widget.