So one of the things you will have noticed if you've ever installed an Android
application that you downloaded from the Android marketplace is that before you
actually install it, it's going to present to you, "Hey,
these are the permissions that this application is asking for."
So for instance, let's see this application wanted to access the camera on the device.
It's going to present you with something saying, "Hey, just so you know, this
application you are downloading is going to access the camera, so you better
trust where it's coming from."
So if you want your application to be able to access certain things on the
device, you need to explicitly say, "I need permission to use this particular thing."
So let's look at an example here.
I just created a blank project, haven't done anything with it, and we're
going to do something where
we are actually going to test the Wi-Fi state of the device.
In my application I need to know, hey!
Is the user's Wi-Fi connected?
Are they able to actually connect to the Internet through Wi-Fi?
So all I am going to do is go into my Main.java file and in the onCreate here, what
I am going to do is I am going to try to access the Wi-Fi state.
So the way in which we do that is I need to create an instance of the
ConnectivityManager, and I am going to call this conman for short, is equal
to--and anytime we need to get a service from the system, so this is
actually a service,
we need to actually ask to get access to that service, and to do that, we use the
getSystemService method. And now we need to put in, what is the name of the
service that we want to actually get to?
Well, we are going to reference it by way of a static constant in the Context
class, and that is Context.CONNECTIVITY_SERVICE.
So here we are basically asking for permission to get access to that service.
So I have a quick fix, which is basically I need to add a cast to that, which is fine.
Okay, so now that I have reference to the ConnectivityManager, what I want to
do is I have a TextView in my XML file, my UI, and I just want to print to that
whether or not the Wi-Fi is connected or not.
So let's go over to my XML file.
Now when you first create an application here, the default text view, it doesn't
have an ID associated with it,
so we need to give it one.
So the way in which we programmatically give an ID to an item here is we
obviously want to set the id attribute.
What we would think we would do is just to say @id/ and then mytextview.
But this is how you reference something that already exists.
So instead what we do is to say, @+, and this basically means we want to add this
id into our resource file.
So now that we have this, we can go back here, and now let's get a reference to that TextView.
So tv = findViewById, and now R.id.mytextview, and let's go ahead and cast that to TextView.
So now we need to figure out, okay is the Wi-Fi actually working or not?
So what we are going to do is to create a Boolean value here,
which is just a true or false value, and I'm going to call it wifi. And now we are
going to use the ConnectivityManager to get that information.
So conman, and then we are going to say, call the getNetworkInfo method, and this
is going to request some information from the system about the network.
Now it's asked us, what type of network information do we want?
Well, we want Wi-Fi, so we are going to pass in a static constant of the
ConnectivityManager class, and that is TYPE_WIFI.
So this is going to get us the information about the Wi-Fi.
If we want just a Boolean true or false about whether it's connected or not,
we can actually then use isConnectedOrConnecting method here, which will return a Boolean.
So again, this long statement is basically asking the system, hey,
we have Wi-Fi or not? And we are storing it in this Boolean value.
So now I will actually use that to actually print out the text.
So I am just going to say, if(wifi) which is basically saying if Wi-Fi is true,
we are going to set the text of that TextView, so tv.setText, and we are going to
say, "This wifi is on."
Now obviously here for the else statement, we are going to say
tv.setText("The wifi is off").
Okay so now let's run this and see what happens.
So we are going to run it as an Android application.
Let's go to the emulator, wait for this to launch up, and you can see we have an error.
Now this is expected, based on what I told you before.
But let's actually go over to DDMS and go into LogCat, and we can see what the
problem is here is that we have a SecurityException.
Basically, we have not asked permission to access the network state.
So this is what we have to add to our manifest file.
So let's go into the manifest. So inside of the manifest we can see we have this
Permissions tab, so let's click that.
And now what we basically want to do is we want to advertise the fact to the
user that hey, can we have permission to access the state of the network?
So what we are going to do here is we are going to click Add and what we want to
choose is this Uses Permission.
We are basically going to say hey, this application uses this
particular permission.
So we will do that, and now we can click this pulldown, and we can see all the
different permissions in which we can request access to in our device.
So for this one, we just wanted to choose ACCESS_NETWORK_STATE. And now if we go
to the XML, we can see this,
this uses-permission tag that's put in.
So now when the user goes to install this application, it's going to warn them,
hey this application wants to access the network state.
So let's go ahead and run this now and see what happens on the emulator.
And you can see now we have our TextView and it says, "The wifi is off," because
on the emulator here it's actually using my network connection to emulate a
mobile data network.
So this is actually the expected behavior.
But you can see what happens if you want to use a specific API or piece of
hardware on the phone, oftentimes you are going to have to explicitly put in the
request to access that. And this again will warn the user before they install
your application that they wanted, because you don't want applications accessing
your camera without you knowing about it, and things of that nature.
So that's the basics of using permissions here.