1 00:00:00,280 --> 00:00:04,580 We've completed our content provider and we're using it to serve up our data. 2 00:00:04,580 --> 00:00:09,120 But so far, it's only being used by this one app and there's not much reason 3 00:00:09,120 --> 00:00:13,360 to use a content provider if you're only going to use it in one app. 4 00:00:13,360 --> 00:00:15,690 So, let's make another. 5 00:00:15,690 --> 00:00:16,810 Let's create a new project. 6 00:00:21,946 --> 00:00:26,987 And let's name it ContentProviderAccessor. 7 00:00:28,255 --> 00:00:32,690 Then let's accept all the defaults and create the project. 8 00:00:38,390 --> 00:00:42,742 To keep things simple we're just going to steal all the code we need from the other 9 00:00:42,742 --> 00:00:43,320 project. 10 00:00:44,340 --> 00:00:45,800 Let's start with the layout. 11 00:00:45,800 --> 00:00:49,610 For this app we're only going to show the list part of the UI. 12 00:00:49,610 --> 00:00:55,389 So over in content providers activity main.xml, let's just copy the scroll view, 13 00:01:04,036 --> 00:01:06,450 And then end Content Provider accessor, 14 00:01:09,171 --> 00:01:12,817 Let's just paste it in over the Hello World! 15 00:01:12,817 --> 00:01:13,446 TextView. 16 00:01:13,446 --> 00:01:17,857 Grooving on to main activity, let's copy over the update list and 17 00:01:17,857 --> 00:01:19,839 get new TextView functions. 18 00:01:37,079 --> 00:01:41,370 Then let's also copy over our linear layout field and the line where we set it. 19 00:01:56,112 --> 00:01:59,610 Finally, let's add a call to update list at the bottom of onCreate. 20 00:02:05,480 --> 00:02:09,960 All that's left is filling in the content URI and the column names. 21 00:02:09,960 --> 00:02:12,210 So let's get rid of these three classes that don't exist. 22 00:02:16,041 --> 00:02:19,330 And then let's copy over our database information from database helper. 23 00:02:31,622 --> 00:02:34,150 And then our content URI from our content provider. 24 00:02:44,818 --> 00:02:47,110 And let's delete this reference to database helper. 25 00:02:52,593 --> 00:02:54,183 Now when we run this new app, 26 00:02:54,183 --> 00:02:57,510 we should be able to see the data from our content provider. 27 00:03:06,990 --> 00:03:10,145 But also, we might get an error. 28 00:03:10,145 --> 00:03:12,270 And if we look through the log 29 00:03:24,375 --> 00:03:29,097 It looks like we got a security exception caused by a denied permission and 30 00:03:29,097 --> 00:03:38,400 then it says it's opening our provider But that it's not exported. 31 00:03:39,480 --> 00:03:43,250 If you want a content provider to be available to other applications, 32 00:03:43,250 --> 00:03:45,510 then it needs to be exported. 33 00:03:45,510 --> 00:03:48,790 To do this we just have to set the exported flag in our manifest. 34 00:03:50,080 --> 00:03:54,870 So over in our original app In the manifest, 35 00:03:54,870 --> 00:04:00,260 inside our provider, let's add the exported flag and set it to true. 36 00:04:01,530 --> 00:04:05,040 Then let's run this app to update the content provider on the device. 37 00:04:10,918 --> 00:04:13,542 And once that's done, let's run the other app and 38 00:04:13,542 --> 00:04:15,810 hopefully we should be able to see our data. 39 00:04:23,443 --> 00:04:28,180 Awesome, we just accessed data that lives in an entirely different process. 40 00:04:28,180 --> 00:04:30,500 And we did it by using a content provider. 41 00:04:30,500 --> 00:04:33,090 After all that's exactly what they're for. 42 00:04:33,090 --> 00:04:36,850 If you want to connect data from one process with code from another, 43 00:04:36,850 --> 00:04:39,300 that's your cue to use a content provider. 44 00:04:39,300 --> 00:04:42,290 On the other hand if everything's staying in one app, 45 00:04:42,290 --> 00:04:45,900 you can still use a content provider but I probably wouldn't. 46 00:04:45,900 --> 00:04:47,190 If you've got any questions or 47 00:04:47,190 --> 00:04:51,670 ideas about how to use a content provider, be sure to post them in the community. 48 00:04:51,670 --> 00:04:52,360 Until next time.