This workshop will be retired on May 1, 2025.
Heads up! To view this whole video, sign in with your Courses Plus account or enroll in your free 7-day trial. Sign In Enroll
Preview
Video Player
00:00
00:00
00:00
- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
In this video we'll use our ContentProvider to provide content to another app!
Related Links
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
We've completed our content provider and
we're using it to serve up our data.
0:00
But so far, it's only being used by
this one app and there's not much reason
0:04
to use a content provider if you're
only going to use it in one app.
0:09
So, let's make another.
0:13
Let's create a new project.
0:15
And let's name it ContentProviderAccessor.
0:21
Then let's accept all the defaults and
create the project.
0:28
To keep things simple we're just going to
steal all the code we need from the other
0:38
project.
0:42
Let's start with the layout.
0:44
For this app we're only going
to show the list part of the UI.
0:45
So over in content providers activity
main.xml, let's just copy the scroll view,
0:49
And then end Content Provider accessor,
1:04
Let's just paste it in
over the Hello World!
1:09
TextView.
1:12
Grooving on to main activity,
let's copy over the update list and
1:13
get new TextView functions.
1:17
Then let's also copy over our linear
layout field and the line where we set it.
1:37
Finally, let's add a call to update
list at the bottom of onCreate.
1:56
All that's left is filling in
the content URI and the column names.
2:05
So let's get rid of these three
classes that don't exist.
2:09
And then let's copy over our database
information from database helper.
2:16
And then our content URI
from our content provider.
2:31
And let's delete this
reference to database helper.
2:44
Now when we run this new app,
2:52
we should be able to see the data
from our content provider.
2:54
But also, we might get an error.
3:06
And if we look through the log
3:10
It looks like we got a security exception
caused by a denied permission and
3:24
then it says it's opening our provider But
that it's not exported.
3:29
If you want a content provider to
be available to other applications,
3:39
then it needs to be exported.
3:43
To do this we just have to set
the exported flag in our manifest.
3:45
So over in our original
app In the manifest,
3:50
inside our provider, let's add
the exported flag and set it to true.
3:54
Then let's run this app to update
the content provider on the device.
4:01
And once that's done,
let's run the other app and
4:10
hopefully we should be
able to see our data.
4:13
Awesome, we just accessed data that
lives in an entirely different process.
4:23
And we did it by using a content provider.
4:28
After all that's exactly what they're for.
4:30
If you want to connect data from
one process with code from another,
4:33
that's your cue to use a content provider.
4:36
On the other hand if
everything's staying in one app,
4:39
you can still use a content provider but
I probably wouldn't.
4:42
If you've got any questions or
4:45
ideas about how to use a content provider,
be sure to post them in the community.
4:47
Until next time.
4:51
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up