1 00:00:00,000 --> 00:00:04,000 So one of the things you will have noticed if you've ever installed an Android 2 00:00:04,000 --> 00:00:08,000 application that you downloaded from the Android marketplace is that before you 3 00:00:08,000 --> 00:00:11,000 actually install it, it's going to present to you, "Hey, 4 00:00:11,000 --> 00:00:15,000 these are the permissions that this application is asking for." 5 00:00:15,000 --> 00:00:20,000 So for instance, let's see this application wanted to access the camera on the device. 6 00:00:20,000 --> 00:00:23,000 It's going to present you with something saying, "Hey, just so you know, this 7 00:00:23,000 --> 00:00:26,000 application you are downloading is going to access the camera, so you better 8 00:00:26,000 --> 00:00:28,000 trust where it's coming from." 9 00:00:28,000 --> 00:00:32,000 So if you want your application to be able to access certain things on the 10 00:00:32,000 --> 00:00:38,000 device, you need to explicitly say, "I need permission to use this particular thing." 11 00:00:38,000 --> 00:00:40,000 So let's look at an example here. 12 00:00:40,000 --> 00:00:43,000 I just created a blank project, haven't done anything with it, and we're 13 00:00:43,000 --> 00:00:44,000 going to do something where 14 00:00:44,000 --> 00:00:48,000 we are actually going to test the Wi-Fi state of the device. 15 00:00:48,000 --> 00:00:50,000 In my application I need to know, hey! 16 00:00:50,000 --> 00:00:52,000 Is the user's Wi-Fi connected? 17 00:00:52,000 --> 00:00:55,000 Are they able to actually connect to the Internet through Wi-Fi? 18 00:00:55,000 --> 00:01:03,000 So all I am going to do is go into my Main.java file and in the onCreate here, what 19 00:01:03,000 --> 00:01:06,000 I am going to do is I am going to try to access the Wi-Fi state. 20 00:01:06,000 --> 00:01:10,000 So the way in which we do that is I need to create an instance of the 21 00:01:10,000 --> 00:01:19,000 ConnectivityManager, and I am going to call this conman for short, is equal 22 00:01:19,000 --> 00:01:24,000 to--and anytime we need to get a service from the system, so this is 23 00:01:24,000 --> 00:01:25,000 actually a service, 24 00:01:25,000 --> 00:01:31,000 we need to actually ask to get access to that service, and to do that, we use the 25 00:01:31,000 --> 00:01:36,000 getSystemService method. And now we need to put in, what is the name of the 26 00:01:36,000 --> 00:01:38,000 service that we want to actually get to? 27 00:01:38,000 --> 00:01:45,000 Well, we are going to reference it by way of a static constant in the Context 28 00:01:45,000 --> 00:01:51,000 class, and that is Context.CONNECTIVITY_SERVICE. 29 00:01:51,000 --> 00:01:56,000 So here we are basically asking for permission to get access to that service. 30 00:01:56,000 --> 00:02:01,000 So I have a quick fix, which is basically I need to add a cast to that, which is fine. 31 00:02:01,000 --> 00:02:05,000 Okay, so now that I have reference to the ConnectivityManager, what I want to 32 00:02:05,000 --> 00:02:10,000 do is I have a TextView in my XML file, my UI, and I just want to print to that 33 00:02:10,000 --> 00:02:13,000 whether or not the Wi-Fi is connected or not. 34 00:02:13,000 --> 00:02:16,000 So let's go over to my XML file. 35 00:02:16,000 --> 00:02:20,000 Now when you first create an application here, the default text view, it doesn't 36 00:02:20,000 --> 00:02:22,000 have an ID associated with it, 37 00:02:22,000 --> 00:02:24,000 so we need to give it one. 38 00:02:24,000 --> 00:02:29,000 So the way in which we programmatically give an ID to an item here is we 39 00:02:29,000 --> 00:02:33,000 obviously want to set the id attribute. 40 00:02:33,000 --> 00:02:40,000 What we would think we would do is just to say @id/ and then mytextview. 41 00:02:40,000 --> 00:02:42,000 But this is how you reference something that already exists. 42 00:02:42,000 --> 00:02:48,000 So instead what we do is to say, @+, and this basically means we want to add this 43 00:02:48,000 --> 00:02:52,000 id into our resource file. 44 00:02:52,000 --> 00:02:57,000 So now that we have this, we can go back here, and now let's get a reference to that TextView. 45 00:02:57,000 --> 00:03:10,000 So tv = findViewById, and now R.id.mytextview, and let's go ahead and cast that to TextView. 46 00:03:10,000 --> 00:03:14,000 So now we need to figure out, okay is the Wi-Fi actually working or not? 47 00:03:14,000 --> 00:03:18,000 So what we are going to do is to create a Boolean value here, 48 00:03:18,000 --> 00:03:23,000 which is just a true or false value, and I'm going to call it wifi. And now we are 49 00:03:23,000 --> 00:03:26,000 going to use the ConnectivityManager to get that information. 50 00:03:26,000 --> 00:03:33,000 So conman, and then we are going to say, call the getNetworkInfo method, and this 51 00:03:33,000 --> 00:03:37,000 is going to request some information from the system about the network. 52 00:03:37,000 --> 00:03:40,000 Now it's asked us, what type of network information do we want? 53 00:03:40,000 --> 00:03:46,000 Well, we want Wi-Fi, so we are going to pass in a static constant of the 54 00:03:46,000 --> 00:03:50,000 ConnectivityManager class, and that is TYPE_WIFI. 55 00:03:50,000 --> 00:03:54,000 So this is going to get us the information about the Wi-Fi. 56 00:03:54,000 --> 00:03:59,000 If we want just a Boolean true or false about whether it's connected or not, 57 00:03:59,000 --> 00:04:05,000 we can actually then use isConnectedOrConnecting method here, which will return a Boolean. 58 00:04:05,000 --> 00:04:09,000 So again, this long statement is basically asking the system, hey, 59 00:04:09,000 --> 00:04:12,000 we have Wi-Fi or not? And we are storing it in this Boolean value. 60 00:04:12,000 --> 00:04:16,000 So now I will actually use that to actually print out the text. 61 00:04:16,000 --> 00:04:21,000 So I am just going to say, if(wifi) which is basically saying if Wi-Fi is true, 62 00:04:21,000 --> 00:04:28,000 we are going to set the text of that TextView, so tv.setText, and we are going to 63 00:04:28,000 --> 00:04:34,000 say, "This wifi is on." 64 00:04:34,000 --> 00:04:38,000 Now obviously here for the else statement, we are going to say 65 00:04:38,000 --> 00:04:47,000 tv.setText("The wifi is off"). 66 00:04:47,000 --> 00:04:50,000 Okay so now let's run this and see what happens. 67 00:04:50,000 --> 00:04:53,000 So we are going to run it as an Android application. 68 00:04:53,000 --> 00:05:00,000 Let's go to the emulator, wait for this to launch up, and you can see we have an error. 69 00:05:00,000 --> 00:05:03,000 Now this is expected, based on what I told you before. 70 00:05:03,000 --> 00:05:10,000 But let's actually go over to DDMS and go into LogCat, and we can see what the 71 00:05:10,000 --> 00:05:14,000 problem is here is that we have a SecurityException. 72 00:05:14,000 --> 00:05:19,000 Basically, we have not asked permission to access the network state. 73 00:05:19,000 --> 00:05:22,000 So this is what we have to add to our manifest file. 74 00:05:22,000 --> 00:05:27,000 So let's go into the manifest. So inside of the manifest we can see we have this 75 00:05:27,000 --> 00:05:30,000 Permissions tab, so let's click that. 76 00:05:30,000 --> 00:05:33,000 And now what we basically want to do is we want to advertise the fact to the 77 00:05:33,000 --> 00:05:38,000 user that hey, can we have permission to access the state of the network? 78 00:05:38,000 --> 00:05:43,000 So what we are going to do here is we are going to click Add and what we want to 79 00:05:43,000 --> 00:05:45,000 choose is this Uses Permission. 80 00:05:45,000 --> 00:05:48,000 We are basically going to say hey, this application uses this 81 00:05:48,000 --> 00:05:50,000 particular permission. 82 00:05:50,000 --> 00:05:54,000 So we will do that, and now we can click this pulldown, and we can see all the 83 00:05:54,000 --> 00:05:58,000 different permissions in which we can request access to in our device. 84 00:05:58,000 --> 00:06:04,000 So for this one, we just wanted to choose ACCESS_NETWORK_STATE. And now if we go 85 00:06:04,000 --> 00:06:07,000 to the XML, we can see this, 86 00:06:07,000 --> 00:06:10,000 this uses-permission tag that's put in. 87 00:06:10,000 --> 00:06:13,000 So now when the user goes to install this application, it's going to warn them, 88 00:06:13,000 --> 00:06:16,000 hey this application wants to access the network state. 89 00:06:16,000 --> 00:06:23,000 So let's go ahead and run this now and see what happens on the emulator. 90 00:06:23,000 --> 00:06:27,000 And you can see now we have our TextView and it says, "The wifi is off," because 91 00:06:27,000 --> 00:06:31,000 on the emulator here it's actually using my network connection to emulate a 92 00:06:31,000 --> 00:06:32,000 mobile data network. 93 00:06:32,000 --> 00:06:34,000 So this is actually the expected behavior. 94 00:06:34,000 --> 00:06:40,000 But you can see what happens if you want to use a specific API or piece of 95 00:06:40,000 --> 00:06:44,000 hardware on the phone, oftentimes you are going to have to explicitly put in the 96 00:06:44,000 --> 00:06:49,000 request to access that. And this again will warn the user before they install 97 00:06:49,000 --> 00:06:52,000 your application that they wanted, because you don't want applications accessing 98 00:06:52,000 --> 00:06:56,000 your camera without you knowing about it, and things of that nature. 99 00:06:56,000 --> 00:06:58,000 So that's the basics of using permissions here.