Before we go ahead and create our own
content provider, it's a good idea to
start by using one to see exactly what a
content provider is and what it offers
and how we can take advantage of it.
Now Android includes quite a few content
providers to allow apps to access data
that's stored on the device now the one
that we're going to be using will allow
us to get access to the users contacts
database. When you make a phone call or
send a text message you can see all your
contacts and search through them if the
contacts database was only available by
the contacts app that it wouldn't really
be a lot of use and also wouldn't make
much sense for all apps like phone or
messaging for example to maintain their
own list of contacts, so the contacts app
has registered a content provider and
that allows other apps to access the
phone's contacts provided they request
the necessary permissions and a user
grants them of course and we'll be
seeing how to do that as we write this
up this isn't going to look very
impressive
if there are no context to display
though and our emulators don't have any
context data on them at the moment.
Now rather than wait until we come to run
the app before setting up some test data
on the emulators I thought I'd get that
out of the way first so we're not
breaking the flow and having to do all
this while trying to understand how
content providers work so can I come
over and start the Android virtual
device manager in endured studio we're
going to create a couple of emulators.
Now you've probably got at least one set
up but watch this video anyway so you
understand how things may work slightly
differently depending on which system
image you used to create it so what we
need for this exercise are two emulators
so that we can see the differences
between API 23 and above
that's the Android marshmallow to oreo at
the moment but also versions prior to
marshmallow. Now we're only got an API 26
emulator set up I've actually got two as
you can see there but I'm going to
change and create a new emulator for API
27 and then we're going to create
another one for versions prior or
earlier than API 23 you on epub or API
21 for that purpose so again if you've
already got an emulator that's using
anything from API 16 to APR 22 and
there's no need to create another one
unless you want to, but I'm going to do
that in this video.
So I'm going to click on create virtual
device then we'll quickly run through we
are creating this new device using API 21
so I'm going to select the nexus 5x
click on next then on the x86 Images tab
here I'm going to click on that and I'm
going to come down and choose API 21 making
sure that I'm choosing Android Google API option
the Android 5.0 Google APIs. Now if
you've already created an emulator using
the system image that doesn't include
the Google API then that's fine we'll be
seeing the difference that makes to the
contacts in a minute. In fact that's the
only reason I'm choosing the Google APIs
here, so that I can show you the
slight difference in behavior when we
try to access the contacts, so there's
really no need to create another API 21
emulator if you already have one, you can
of course but you may want to save some
space, so take up about a key a
gigabyte each. Now the GenyMotion
emulators work a lot better than the ARM
system images that will work fine for
this section if you want to use them
instead we've stopped providing links
though to the GenyMotion emulators
because the site changes too frequently
you want the emulator that says free for
personal use while you're learning from
genie motion if you decided to go down
that route. Once you start creating and
distributing your own apps then you'll
need to pay for a license but generally
I would recommend is certain if you've
got it until CPU and your computer that
the images that come with Android studio
are more than sufficient and certainly
fast enough it's only when you get into
the situation of having an AMD CPU or an
Intel CPU you have to use these other
images these ARM images which are just
too slow to be practical. Alright so
I've chosen the lollipop 21 x86 making
sure I've selected the Google API
version there, I'm gonna click on next
and I suggest you change the AVD name
here what I'm going to do is just go
with Nexus 5x APR 21 by gonna call this
context just so it's really clear that
this is the emulator that I've set up
for that purpose and okay I'll click on
finish brother and we're done
I'll give it a moment to more complete
and we've now got our Nexus 5x APR 21
contacts emulator set up I'm gonna
immediately start that up they're going
to close down
the virtual device window and we'll
bring back the emulator not still
loading up, we'll just make this a little
bit bigger. Okay so I'm gonna click on ok,
there so next whether they'll launch the
contacts app and create a few contacts
for testing. Now here's where things get
a bit different if your emulator uses a
system image with the Google API which
I've actually set up here. Now if your
image doesn't use the Google API you'll
have a screen with three buttons let me
just start this so you can see I could
have clicked it from the main screen
when I was coming into here and clicking
on contacts so basically if you didn't
use the Google API was you'd have a
screen immediately with three buttons
with the top one letting you create a
new contact so if that's the case please
bear with us while the rest of us catch
up, now if you've included the Google API
so I have then you'll be asked to sign
in to the device before you can work
with the contacts like it's doing here
on my screen, so I'll pause the video now,
now while I enter my details on the
screen and I'm going to click on next
and we'll come back and continue.
Alright so after entering the username and
password you get to this screen so we
can click on more down here to see more
information and we can click on yes I'm
in or accept, yes I'm in accept to
accept the terms we then get to this
screen after that. Now I'm using a test
account but if you're using your live
gmail account with all your real contact
details then I'd suggest you don't let
Apps access them until after we've
tested them testing on your live data
could be a good way to lose your
contacts. Now this happens only reading
the contacts data not writing to it but
even so it's a good habit to get into
don't test apps using production
databases always work with test data
until you're sure it's working, so it's a
good idea to create another gmail
account for testing that way you don't
risk messing up your real data so what
I'm going to do is uncheck this box or
will actually turn this off by sliding
this little slider over here so that
it's not backing up to Google Drive I'm
going to scroll down and click on next
and now we're all on the same screen
this is where the contacts app starts if
you haven't included the Google API is
in the system image. Alright so let's
create a new contact by clicking on this
top button create a new contact now the
first time I do that I get this screen
giving an option to back up the contacts
that we add or at least I would get that
unless I'm logged into Google in this
account you can see it's automatically
saying your new account will be
synchronized and it shows synchronized
with and whatever gmail account that you
use to sign in to this device this
emulator, you may also be prompted if you
weren't able to login successfully to
keep local so I would normally recommend
you to keep local in that scenario but
in this case I'm just going to click on
ok and that brings us to the Google
contact moon page now. So once again I'm
advising against using your normal
Google Account on an emulator if you do
manage to set it to sync the data so if
you did get the option to make sure you
choose keep local. Alright so now that
we're on this screen now I'm not gonna
enter data for the email and phone
number at this stage because there any
displaying the name anyhow, so what I'm
going to do is just create a couple of
entries I'm going to top mark submit
this the first one and then I'm gonna do
another one so I'm gonna click on the
little tick here to save it known as
noting now that time we've got an entry
out of there it's nothing about it that
you can see here this add phone number
net email I'm not going to add those for
Mike in his case we're really only going
to be displaying the name in our test
app anyway so I'm gonna click on back
then then I'm going to click on the
button down the bottom right to add
another contact let's call this one Bob
Brown and click on the little tick there
to say that to come back and you can see
we've now got two entries in our
contacts now and that should do for now
you can do two more if you want to but
that's enough for our demonstration.
Now we also want to run the app on an API 23
or higher emulator so I'm going to go
and repeat the process now to add the
same contact details to an emulator
running Android Audio API 27 in this
case so I'm going to close this down
then what I'll actually do is after we
go on create the emulator, I'll fast
forward this just so it doesn't take too
long to finish, you can see that I've got
an API 26 emulator
we're not going to create a new API 27,
so going to go through the same process
I'm going to choose to go to x86 images
then we want to choose the Oreo Android
8.1 Google API so I'm choosing the
latest API there, next I'm going to do
the same thing there I'm going to put
contacts just so we can differentiate
click on finish
I'm going to start that new emulator so
close down the virtual devices page
like this a bit bigger what sliding okay
now it's loaded we're going to click on
the button click on contacts I click on
plus here it says take a minute to add
an account that will backup your
contacts to Google this is API 27 I'm
going to click on cancel there I don't
want to actually add one for to add our
accounts to Google it's just a quicker
way to know that we can access it you
can see that it's different to what the
process was for our Android lollipop
device, so I'm Mike Smith notice also
that we've got a first in the last name
column that's a little bit different I'm
gonna click on save up here there's mark
you can see the interface is a little
bit different because we're running a
later API version, click on the plus and
let's add Bob Brown as well and click on
save and we actually got the two entries
and you probably saw that anyway about
noting that because I didn't add a
Google Account saving to devices as I
specified there, alright so I'm going to
click on back at the moment.
Alright, so done through all this now so
that you might be asking at this point
what did we create to emulate API
23 one the Android lollipop one and the
Android Oreo one. Well the reason that
we've added this second one the API 23
or higher emulator the API 27 in our
case is that Google changed the way that
permissions worked in Android
marshmallow and that was API 23,
once we've seen the app running on API 21
we'll have to look at what we need to
change in our code to make it work on
Android versions from API 23 on, so we
need two emulators do we have to do that
and I've gone through all this process
now so that when we need this data it'll
be there ready for use and we don't have
to get distracted from what we're doing
when we come to run the app, so make sure
you remember which emulators you've
added these contacts in and you can see
in my case that I just named them with
the word contacts at the end of the
emulator name because you'll need to get
access to those for our testing purposes.
So let's end the video here and we'll
make a start on using the contact
provider in the next video.