Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this video we'll learn about the different types of permissions and start the project!
Button
<Button
android:text="Call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/callButton"/>
Make Call Function
private void makeCall() {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + "12345678"));
startActivity(intent);
}
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
Hi, I'm Ben and in this workshop we're
going to talk about permissions.
0:04
Permissions and
Android used to be super simple.
0:09
You just declare the permissions
you need in your apps manifest and
0:12
then your users would accept those
permissions when they install the app.
0:15
But with Marshmallow, things changed.
0:19
Now, users could just grant permissions
to apps while the app is running,
0:21
instead of when they
first install the app.
0:25
This way,
the app install process is much smoother.
0:27
And it also gives users the chance to
allow or disallow certain permissions.
0:30
For example, maybe there's a new
camera app that you'd like to try.
0:35
Obviously, you want to give this
app permission to use the camera.
0:39
But maybe you're not so
keen on giving it access to your location.
0:42
To see how to use this
new permissions model,
0:47
we're going to create a simple
app that calls a phone number.
0:49
But before we get to the app,
let's take a step back and
0:52
talk about permissions in general.
0:55
In Android there's two different
categories of permission, normal and
0:57
dangerous.
1:01
Normal permissions don't risk giving
access to users private data.
1:02
So, if you list a normal
permission in your apps manifest,
1:06
it gets granted
automatically by the system.
1:10
On the other hand,
1:13
dangerous permissions can give the app
access to a user's private data.
1:14
So, if you list a dangerous
permission in your apps manifest
1:18
the user will need to explicitly
approve the permission.
1:21
If you'd like to read more about which
permissions are considered dangerous and
1:25
which are considered normal, take a look
at the link in the teachers notes below.
1:28
Now that we've got that covered,
let's get back to creating an app, so
1:32
we can see this all in action.
1:35
Let's start by creating a new project,
and let's name it PermissionsApp.
1:37
And let's accept all the defaults.
1:45
Next, let's update the layout to have
a call button right in the middle.
1:53
To do this let's,
2:00
just replace this text view, With the
button down below in the teacher's notes.
2:00
Now that we've got our button we just
need to make it call a phone number
2:09
when we click it.
2:12
Over the main activity, let's add
some space at the bottom of onCreate,
2:13
and then let's create a new variable for
a button.
2:17
Button let's call it
callButton = (Button) and
2:21
you've got id, (R.id.callButton).
2:27
Then let's add in on click listener,
2:33
callButton.setOnClickListener
(new_View.OnClickListener().
2:36
And inside the OnClickListener,
let's call a phone number by copying in
2:43
the method from the teacher's notes below,
and then calling it.
2:47
So, paste in the makeCall method, then use
Alt+Enter to import the pieces we need.
2:54
And then let's call this method
inside our OnClick method.
3:00
So, when we tap on our button
it should call this number.
3:04
Great, and right.
3:08
We're going to need to add the call
phone permission to our manifest.
3:10
So, let's go over to our manifest,
and right above the application tag
3:14
let's add a uses-permission tag, and
3:19
let's set android:name to have
the CALL_PHONE permission.
3:24
Then, let's close the tag on the same
line, and let's see what we've got.
3:28
Back in MainActivity we've
still got the error, but
3:33
before we move on, let me show you how
this looks on two different API levels.
3:38
I've currently got two emulators running,
one on Marshmallow and
3:43
the other on Lollipop.
3:46
Running this on the Marshmallow device.
3:48
We get the CALL button, and
when we click it the app crashes.
3:57
We get a permission denial
SecurityException, and
4:09
if we run it on the Lollipop device,
4:12
And click the CALL button.
4:24
It just works!
4:26
So as expected, we've got a bit more
work to do to support Marshmallow,
4:28
which we'll get to in the next video.
4:32
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