1 00:00:04,530 --> 00:00:07,020 Before we go ahead and create our own 2 00:00:07,020 --> 00:00:09,570 content provider, it's a good idea to 3 00:00:09,570 --> 00:00:11,670 start by using one to see exactly what a 4 00:00:11,670 --> 00:00:13,500 content provider is and what it offers 5 00:00:13,500 --> 00:00:15,929 and how we can take advantage of it. 6 00:00:15,929 --> 00:00:18,119 Now Android includes quite a few content 7 00:00:18,119 --> 00:00:20,550 providers to allow apps to access data 8 00:00:20,550 --> 00:00:23,430 that's stored on the device now the one 9 00:00:23,430 --> 00:00:24,720 that we're going to be using will allow 10 00:00:24,720 --> 00:00:27,119 us to get access to the users contacts 11 00:00:27,119 --> 00:00:29,580 database. When you make a phone call or 12 00:00:29,580 --> 00:00:31,710 send a text message you can see all your 13 00:00:31,710 --> 00:00:33,809 contacts and search through them if the 14 00:00:33,809 --> 00:00:35,760 contacts database was only available by 15 00:00:35,760 --> 00:00:37,680 the contacts app that it wouldn't really 16 00:00:37,680 --> 00:00:39,899 be a lot of use and also wouldn't make 17 00:00:39,899 --> 00:00:42,030 much sense for all apps like phone or 18 00:00:42,030 --> 00:00:44,280 messaging for example to maintain their 19 00:00:44,280 --> 00:00:47,010 own list of contacts, so the contacts app 20 00:00:47,010 --> 00:00:49,680 has registered a content provider and 21 00:00:49,680 --> 00:00:51,750 that allows other apps to access the 22 00:00:51,750 --> 00:00:54,300 phone's contacts provided they request 23 00:00:54,300 --> 00:00:56,399 the necessary permissions and a user 24 00:00:56,399 --> 00:00:58,110 grants them of course and we'll be 25 00:00:58,110 --> 00:00:59,520 seeing how to do that as we write this 26 00:00:59,520 --> 00:01:01,379 up this isn't going to look very 27 00:01:01,379 --> 00:01:01,980 impressive 28 00:01:01,980 --> 00:01:03,899 if there are no context to display 29 00:01:03,899 --> 00:01:06,420 though and our emulators don't have any 30 00:01:06,420 --> 00:01:08,610 context data on them at the moment. 31 00:01:08,610 --> 00:01:10,350 Now rather than wait until we come to run 32 00:01:10,350 --> 00:01:11,909 the app before setting up some test data 33 00:01:11,909 --> 00:01:13,830 on the emulators I thought I'd get that 34 00:01:13,830 --> 00:01:15,630 out of the way first so we're not 35 00:01:15,630 --> 00:01:17,729 breaking the flow and having to do all 36 00:01:17,729 --> 00:01:18,990 this while trying to understand how 37 00:01:18,990 --> 00:01:22,020 content providers work so can I come 38 00:01:22,020 --> 00:01:23,340 over and start the Android virtual 39 00:01:23,340 --> 00:01:27,000 device manager in endured studio we're 40 00:01:27,000 --> 00:01:28,409 going to create a couple of emulators. 41 00:01:28,409 --> 00:01:30,960 Now you've probably got at least one set 42 00:01:30,960 --> 00:01:32,970 up but watch this video anyway so you 43 00:01:32,970 --> 00:01:34,770 understand how things may work slightly 44 00:01:34,770 --> 00:01:37,049 differently depending on which system 45 00:01:37,049 --> 00:01:39,540 image you used to create it so what we 46 00:01:39,540 --> 00:01:41,369 need for this exercise are two emulators 47 00:01:41,369 --> 00:01:43,650 so that we can see the differences 48 00:01:43,650 --> 00:01:45,570 between API 23 and above 49 00:01:45,570 --> 00:01:47,340 that's the Android marshmallow to oreo at 50 00:01:47,340 --> 00:01:49,979 the moment but also versions prior to 51 00:01:49,979 --> 00:01:52,860 marshmallow. Now we're only got an API 26 52 00:01:52,860 --> 00:01:55,079 emulator set up I've actually got two as 53 00:01:55,079 --> 00:01:56,520 you can see there but I'm going to 54 00:01:56,520 --> 00:01:59,009 change and create a new emulator for API 55 00:01:59,009 --> 00:02:00,960 27 and then we're going to create 56 00:02:00,960 --> 00:02:03,149 another one for versions prior or 57 00:02:03,149 --> 00:02:06,329 earlier than API 23 you on epub or API 58 00:02:06,329 --> 00:02:08,850 21 for that purpose so again if you've 59 00:02:08,850 --> 00:02:10,229 already got an emulator that's using 60 00:02:10,229 --> 00:02:13,260 anything from API 16 to APR 22 and 61 00:02:13,260 --> 00:02:14,790 there's no need to create another one 62 00:02:14,790 --> 00:02:16,500 unless you want to, but I'm going to do 63 00:02:16,500 --> 00:02:17,970 that in this video. 64 00:02:17,970 --> 00:02:19,440 So I'm going to click on create virtual 65 00:02:19,440 --> 00:02:22,110 device then we'll quickly run through we 66 00:02:22,110 --> 00:02:24,420 are creating this new device using API 21 67 00:02:24,420 --> 00:02:26,750 so I'm going to select the nexus 5x 68 00:02:26,750 --> 00:02:31,890 click on next then on the x86 Images tab 69 00:02:31,890 --> 00:02:33,450 here I'm going to click on that and I'm 70 00:02:33,450 --> 00:02:36,440 going to come down and choose API 21 making 71 00:02:36,440 --> 00:02:40,140 sure that I'm choosing Android Google API option 72 00:02:40,140 --> 00:02:43,440 the Android 5.0 Google APIs. Now if 73 00:02:43,440 --> 00:02:45,120 you've already created an emulator using 74 00:02:45,120 --> 00:02:46,410 the system image that doesn't include 75 00:02:46,410 --> 00:02:49,050 the Google API then that's fine we'll be 76 00:02:49,050 --> 00:02:50,550 seeing the difference that makes to the 77 00:02:50,550 --> 00:02:52,410 contacts in a minute. In fact that's the 78 00:02:52,410 --> 00:02:54,150 only reason I'm choosing the Google APIs 79 00:02:54,150 --> 00:02:55,920 here, so that I can show you the 80 00:02:55,920 --> 00:02:58,020 slight difference in behavior when we 81 00:02:58,020 --> 00:03:00,210 try to access the contacts, so there's 82 00:03:00,210 --> 00:03:02,220 really no need to create another API 21 83 00:03:02,220 --> 00:03:04,200 emulator if you already have one, you can 84 00:03:04,200 --> 00:03:06,090 of course but you may want to save some 85 00:03:06,090 --> 00:03:07,770 space, so take up about a key a 86 00:03:07,770 --> 00:03:10,920 gigabyte each. Now the GenyMotion 87 00:03:10,920 --> 00:03:13,020 emulators work a lot better than the ARM 88 00:03:13,020 --> 00:03:15,090 system images that will work fine for 89 00:03:15,090 --> 00:03:16,410 this section if you want to use them 90 00:03:16,410 --> 00:03:18,600 instead we've stopped providing links 91 00:03:18,600 --> 00:03:20,220 though to the GenyMotion emulators 92 00:03:20,220 --> 00:03:22,440 because the site changes too frequently 93 00:03:22,440 --> 00:03:24,690 you want the emulator that says free for 94 00:03:24,690 --> 00:03:26,250 personal use while you're learning from 95 00:03:26,250 --> 00:03:27,570 genie motion if you decided to go down 96 00:03:27,570 --> 00:03:29,940 that route. Once you start creating and 97 00:03:29,940 --> 00:03:31,709 distributing your own apps then you'll 98 00:03:31,709 --> 00:03:34,200 need to pay for a license but generally 99 00:03:34,200 --> 00:03:35,340 I would recommend is certain if you've 100 00:03:35,340 --> 00:03:38,070 got it until CPU and your computer that 101 00:03:38,070 --> 00:03:39,840 the images that come with Android studio 102 00:03:39,840 --> 00:03:42,150 are more than sufficient and certainly 103 00:03:42,150 --> 00:03:43,410 fast enough it's only when you get into 104 00:03:43,410 --> 00:03:46,110 the situation of having an AMD CPU or an 105 00:03:46,110 --> 00:03:48,600 Intel CPU you have to use these other 106 00:03:48,600 --> 00:03:51,239 images these ARM images which are just 107 00:03:51,239 --> 00:03:54,420 too slow to be practical. Alright so 108 00:03:54,420 --> 00:03:57,720 I've chosen the lollipop 21 x86 making 109 00:03:57,720 --> 00:03:58,980 sure I've selected the Google API 110 00:03:58,980 --> 00:04:01,260 version there, I'm gonna click on next 111 00:04:01,260 --> 00:04:04,530 and I suggest you change the AVD name 112 00:04:04,530 --> 00:04:05,700 here what I'm going to do is just go 113 00:04:05,700 --> 00:04:08,760 with Nexus 5x APR 21 by gonna call this 114 00:04:08,760 --> 00:04:11,040 context just so it's really clear that 115 00:04:11,040 --> 00:04:12,540 this is the emulator that I've set up 116 00:04:12,540 --> 00:04:15,630 for that purpose and okay I'll click on 117 00:04:15,630 --> 00:04:16,890 finish brother and we're done 118 00:04:16,890 --> 00:04:18,959 I'll give it a moment to more complete 119 00:04:18,959 --> 00:04:21,630 and we've now got our Nexus 5x APR 21 120 00:04:21,630 --> 00:04:24,540 contacts emulator set up I'm gonna 121 00:04:24,540 --> 00:04:29,130 immediately start that up they're going 122 00:04:29,130 --> 00:04:31,790 to close down 123 00:04:31,790 --> 00:04:35,190 the virtual device window and we'll 124 00:04:35,190 --> 00:04:36,750 bring back the emulator not still 125 00:04:36,750 --> 00:04:43,530 loading up, we'll just make this a little 126 00:04:43,530 --> 00:04:48,420 bit bigger. Okay so I'm gonna click on ok, 127 00:04:48,420 --> 00:04:51,000 there so next whether they'll launch the 128 00:04:51,000 --> 00:04:53,070 contacts app and create a few contacts 129 00:04:53,070 --> 00:04:55,380 for testing. Now here's where things get 130 00:04:55,380 --> 00:04:57,450 a bit different if your emulator uses a 131 00:04:57,450 --> 00:05:00,030 system image with the Google API which 132 00:05:00,030 --> 00:05:02,130 I've actually set up here. Now if your 133 00:05:02,130 --> 00:05:04,680 image doesn't use the Google API you'll 134 00:05:04,680 --> 00:05:06,960 have a screen with three buttons let me 135 00:05:06,960 --> 00:05:09,060 just start this so you can see I could 136 00:05:09,060 --> 00:05:10,320 have clicked it from the main screen 137 00:05:10,320 --> 00:05:11,820 when I was coming into here and clicking 138 00:05:11,820 --> 00:05:15,120 on contacts so basically if you didn't 139 00:05:15,120 --> 00:05:17,040 use the Google API was you'd have a 140 00:05:17,040 --> 00:05:18,570 screen immediately with three buttons 141 00:05:18,570 --> 00:05:21,390 with the top one letting you create a 142 00:05:21,390 --> 00:05:23,520 new contact so if that's the case please 143 00:05:23,520 --> 00:05:25,320 bear with us while the rest of us catch 144 00:05:25,320 --> 00:05:28,020 up, now if you've included the Google API 145 00:05:28,020 --> 00:05:30,120 so I have then you'll be asked to sign 146 00:05:30,120 --> 00:05:31,800 in to the device before you can work 147 00:05:31,800 --> 00:05:33,690 with the contacts like it's doing here 148 00:05:33,690 --> 00:05:35,700 on my screen, so I'll pause the video now, 149 00:05:35,700 --> 00:05:37,590 now while I enter my details on the 150 00:05:37,590 --> 00:05:38,700 screen and I'm going to click on next 151 00:05:38,700 --> 00:05:41,540 and we'll come back and continue. 152 00:05:41,540 --> 00:05:44,010 Alright so after entering the username and 153 00:05:44,010 --> 00:05:46,350 password you get to this screen so we 154 00:05:46,350 --> 00:05:47,910 can click on more down here to see more 155 00:05:47,910 --> 00:05:51,210 information and we can click on yes I'm 156 00:05:51,210 --> 00:05:56,100 in or accept, yes I'm in accept to 157 00:05:56,100 --> 00:05:59,610 accept the terms we then get to this 158 00:05:59,610 --> 00:06:02,310 screen after that. Now I'm using a test 159 00:06:02,310 --> 00:06:03,840 account but if you're using your live 160 00:06:03,840 --> 00:06:06,240 gmail account with all your real contact 161 00:06:06,240 --> 00:06:08,460 details then I'd suggest you don't let 162 00:06:08,460 --> 00:06:10,470 Apps access them until after we've 163 00:06:10,470 --> 00:06:12,810 tested them testing on your live data 164 00:06:12,810 --> 00:06:13,920 could be a good way to lose your 165 00:06:13,920 --> 00:06:16,410 contacts. Now this happens only reading 166 00:06:16,410 --> 00:06:18,720 the contacts data not writing to it but 167 00:06:18,720 --> 00:06:20,460 even so it's a good habit to get into 168 00:06:20,460 --> 00:06:21,810 don't test apps using production 169 00:06:21,810 --> 00:06:24,240 databases always work with test data 170 00:06:24,240 --> 00:06:26,760 until you're sure it's working, so it's a 171 00:06:26,760 --> 00:06:28,050 good idea to create another gmail 172 00:06:28,050 --> 00:06:30,240 account for testing that way you don't 173 00:06:30,240 --> 00:06:32,490 risk messing up your real data so what 174 00:06:32,490 --> 00:06:34,080 I'm going to do is uncheck this box or 175 00:06:34,080 --> 00:06:35,730 will actually turn this off by sliding 176 00:06:35,730 --> 00:06:37,530 this little slider over here so that 177 00:06:37,530 --> 00:06:40,350 it's not backing up to Google Drive I'm 178 00:06:40,350 --> 00:06:43,670 going to scroll down and click on next 179 00:06:43,670 --> 00:06:46,640 and now we're all on the same screen 180 00:06:46,640 --> 00:06:49,070 this is where the contacts app starts if 181 00:06:49,070 --> 00:06:51,170 you haven't included the Google API is 182 00:06:51,170 --> 00:06:53,480 in the system image. Alright so let's 183 00:06:53,480 --> 00:06:55,700 create a new contact by clicking on this 184 00:06:55,700 --> 00:06:59,990 top button create a new contact now the 185 00:06:59,990 --> 00:07:02,060 first time I do that I get this screen 186 00:07:02,060 --> 00:07:03,830 giving an option to back up the contacts 187 00:07:03,830 --> 00:07:06,080 that we add or at least I would get that 188 00:07:06,080 --> 00:07:07,850 unless I'm logged into Google in this 189 00:07:07,850 --> 00:07:09,110 account you can see it's automatically 190 00:07:09,110 --> 00:07:10,220 saying your new account will be 191 00:07:10,220 --> 00:07:12,410 synchronized and it shows synchronized 192 00:07:12,410 --> 00:07:14,390 with and whatever gmail account that you 193 00:07:14,390 --> 00:07:17,180 use to sign in to this device this 194 00:07:17,180 --> 00:07:19,940 emulator, you may also be prompted if you 195 00:07:19,940 --> 00:07:21,920 weren't able to login successfully to 196 00:07:21,920 --> 00:07:23,840 keep local so I would normally recommend 197 00:07:23,840 --> 00:07:26,510 you to keep local in that scenario but 198 00:07:26,510 --> 00:07:27,620 in this case I'm just going to click on 199 00:07:27,620 --> 00:07:30,800 ok and that brings us to the Google 200 00:07:30,800 --> 00:07:33,680 contact moon page now. So once again I'm 201 00:07:33,680 --> 00:07:35,450 advising against using your normal 202 00:07:35,450 --> 00:07:37,640 Google Account on an emulator if you do 203 00:07:37,640 --> 00:07:39,800 manage to set it to sync the data so if 204 00:07:39,800 --> 00:07:41,030 you did get the option to make sure you 205 00:07:41,030 --> 00:07:43,160 choose keep local. Alright so now that 206 00:07:43,160 --> 00:07:45,050 we're on this screen now I'm not gonna 207 00:07:45,050 --> 00:07:46,430 enter data for the email and phone 208 00:07:46,430 --> 00:07:47,630 number at this stage because there any 209 00:07:47,630 --> 00:07:49,520 displaying the name anyhow, so what I'm 210 00:07:49,520 --> 00:07:50,720 going to do is just create a couple of 211 00:07:50,720 --> 00:07:52,820 entries I'm going to top mark submit 212 00:07:52,820 --> 00:07:55,940 this the first one and then I'm gonna do 213 00:07:55,940 --> 00:07:57,110 another one so I'm gonna click on the 214 00:07:57,110 --> 00:08:00,110 little tick here to save it known as 215 00:08:00,110 --> 00:08:01,670 noting now that time we've got an entry 216 00:08:01,670 --> 00:08:03,230 out of there it's nothing about it that 217 00:08:03,230 --> 00:08:04,880 you can see here this add phone number 218 00:08:04,880 --> 00:08:06,680 net email I'm not going to add those for 219 00:08:06,680 --> 00:08:08,660 Mike in his case we're really only going 220 00:08:08,660 --> 00:08:11,120 to be displaying the name in our test 221 00:08:11,120 --> 00:08:12,560 app anyway so I'm gonna click on back 222 00:08:12,560 --> 00:08:15,050 then then I'm going to click on the 223 00:08:15,050 --> 00:08:16,340 button down the bottom right to add 224 00:08:16,340 --> 00:08:18,740 another contact let's call this one Bob 225 00:08:18,740 --> 00:08:23,360 Brown and click on the little tick there 226 00:08:23,360 --> 00:08:26,120 to say that to come back and you can see 227 00:08:26,120 --> 00:08:28,100 we've now got two entries in our 228 00:08:28,100 --> 00:08:30,050 contacts now and that should do for now 229 00:08:30,050 --> 00:08:31,340 you can do two more if you want to but 230 00:08:31,340 --> 00:08:33,380 that's enough for our demonstration. 231 00:08:33,380 --> 00:08:35,600 Now we also want to run the app on an API 23 232 00:08:35,600 --> 00:08:37,340 or higher emulator so I'm going to go 233 00:08:37,340 --> 00:08:38,690 and repeat the process now to add the 234 00:08:38,690 --> 00:08:41,059 same contact details to an emulator 235 00:08:41,059 --> 00:08:43,940 running Android Audio API 27 in this 236 00:08:43,940 --> 00:08:46,750 case so I'm going to close this down 237 00:08:46,750 --> 00:08:49,010 then what I'll actually do is after we 238 00:08:49,010 --> 00:08:51,320 go on create the emulator, I'll fast 239 00:08:51,320 --> 00:08:52,700 forward this just so it doesn't take too 240 00:08:52,700 --> 00:08:56,120 long to finish, you can see that I've got 241 00:08:56,120 --> 00:08:57,590 an API 26 emulator 242 00:08:57,590 --> 00:08:59,860 we're not going to create a new API 27, 243 00:08:59,860 --> 00:09:02,020 so going to go through the same process 244 00:09:02,020 --> 00:09:05,570 I'm going to choose to go to x86 images 245 00:09:05,570 --> 00:09:09,010 then we want to choose the Oreo Android 246 00:09:09,010 --> 00:09:11,360 8.1 Google API so I'm choosing the 247 00:09:11,360 --> 00:09:14,270 latest API there, next I'm going to do 248 00:09:14,270 --> 00:09:15,320 the same thing there I'm going to put 249 00:09:15,320 --> 00:09:19,130 contacts just so we can differentiate 250 00:09:19,130 --> 00:09:21,100 click on finish 251 00:09:21,100 --> 00:09:27,230 I'm going to start that new emulator so 252 00:09:27,230 --> 00:09:37,160 close down the virtual devices page 253 00:09:37,160 --> 00:09:41,690 like this a bit bigger what sliding okay 254 00:09:41,690 --> 00:09:43,250 now it's loaded we're going to click on 255 00:09:43,250 --> 00:09:47,270 the button click on contacts I click on 256 00:09:47,270 --> 00:09:50,900 plus here it says take a minute to add 257 00:09:50,900 --> 00:09:52,040 an account that will backup your 258 00:09:52,040 --> 00:09:54,110 contacts to Google this is API 27 I'm 259 00:09:54,110 --> 00:09:56,000 going to click on cancel there I don't 260 00:09:56,000 --> 00:09:58,850 want to actually add one for to add our 261 00:09:58,850 --> 00:10:00,680 accounts to Google it's just a quicker 262 00:10:00,680 --> 00:10:02,090 way to know that we can access it you 263 00:10:02,090 --> 00:10:03,410 can see that it's different to what the 264 00:10:03,410 --> 00:10:04,730 process was for our Android lollipop 265 00:10:04,730 --> 00:10:08,450 device, so I'm Mike Smith notice also 266 00:10:08,450 --> 00:10:09,770 that we've got a first in the last name 267 00:10:09,770 --> 00:10:11,660 column that's a little bit different I'm 268 00:10:11,660 --> 00:10:15,530 gonna click on save up here there's mark 269 00:10:15,530 --> 00:10:17,690 you can see the interface is a little 270 00:10:17,690 --> 00:10:18,740 bit different because we're running a 271 00:10:18,740 --> 00:10:21,260 later API version, click on the plus and 272 00:10:21,260 --> 00:10:26,360 let's add Bob Brown as well and click on 273 00:10:26,360 --> 00:10:29,540 save and we actually got the two entries 274 00:10:29,540 --> 00:10:31,070 and you probably saw that anyway about 275 00:10:31,070 --> 00:10:32,690 noting that because I didn't add a 276 00:10:32,690 --> 00:10:35,480 Google Account saving to devices as I 277 00:10:35,480 --> 00:10:36,920 specified there, alright so I'm going to 278 00:10:36,920 --> 00:10:40,090 click on back at the moment. 279 00:10:40,090 --> 00:10:42,290 Alright, so done through all this now so 280 00:10:42,290 --> 00:10:44,390 that you might be asking at this point 281 00:10:44,390 --> 00:10:46,340 what did we create to emulate API 282 00:10:46,340 --> 00:10:49,010 23 one the Android lollipop one and the 283 00:10:49,010 --> 00:10:51,110 Android Oreo one. Well the reason that 284 00:10:51,110 --> 00:10:53,300 we've added this second one the API 23 285 00:10:53,300 --> 00:10:55,730 or higher emulator the API 27 in our 286 00:10:55,730 --> 00:10:58,100 case is that Google changed the way that 287 00:10:58,100 --> 00:10:59,540 permissions worked in Android 288 00:10:59,540 --> 00:11:01,910 marshmallow and that was API 23, 289 00:11:01,910 --> 00:11:03,260 once we've seen the app running on API 21 290 00:11:03,260 --> 00:11:05,330 we'll have to look at what we need to 291 00:11:05,330 --> 00:11:07,550 change in our code to make it work on 292 00:11:07,550 --> 00:11:10,370 Android versions from API 23 on, so we 293 00:11:10,370 --> 00:11:11,840 need two emulators do we have to do that 294 00:11:11,840 --> 00:11:13,730 and I've gone through all this process 295 00:11:13,730 --> 00:11:16,250 now so that when we need this data it'll 296 00:11:16,250 --> 00:11:17,720 be there ready for use and we don't have 297 00:11:17,720 --> 00:11:19,400 to get distracted from what we're doing 298 00:11:19,400 --> 00:11:21,500 when we come to run the app, so make sure 299 00:11:21,500 --> 00:11:23,030 you remember which emulators you've 300 00:11:23,030 --> 00:11:25,190 added these contacts in and you can see 301 00:11:25,190 --> 00:11:26,990 in my case that I just named them with 302 00:11:26,990 --> 00:11:29,090 the word contacts at the end of the 303 00:11:29,090 --> 00:11:30,860 emulator name because you'll need to get 304 00:11:30,860 --> 00:11:32,900 access to those for our testing purposes. 305 00:11:32,900 --> 00:11:34,880 So let's end the video here and we'll 306 00:11:34,880 --> 00:11:36,410 make a start on using the contact 307 00:11:36,410 --> 00:11:40,210 provider in the next video.