In this video, you are going to learn how to request for permissions, and we're going to look at how
to request for one permission, for example, for the camera permission so that we can use the camera
in our application.
But we're also going to look at how we can ask for multiple permissions at once, which means, for
example, the location also and not just a camera permission.
So you see here, I can now give access to one thing.
So, for example, for the camera and then also it will ask me for the permission for the location and
here can say what kind of location I want to give the application.
So the precise location or an approximate location.
So this will really depend on the kind of app you want to build.
But I'm just going to give the approximate location, for example, and I'm just going to give it access
while I'm using the app.
So you see permission was denied for location because I didn't give the location like the final location
of precise location, but I did give the course location.
And you're going to see why that is and how we can set all of that up.
So let's go ahead and look into the project that I prepared here.
So I have this activity main XM L, which just has this one button in there, which just says request
camera permission.
So if you don't know what's up here, you should definitely jump into another lecture of mine where
you learn about XML basics.
So here we request for the camera permission and now inside of our main activity.
That's where we are going to request for the camera permission and for the other permissions also.
And in order to even be able to request for any of the permissions, I need to define that in the Android
Manifest.
So there is this Manifest folder and there's this Android Manifest, some L file and in there you can
on one hand, of course, have all of your activities and defined settings for your application and
activities and stuff.
But what's more important for this video is going to be the permissions that we want to use.
So here you need to create this tag user's permission with the Android name and then the permission
that you want to use in the application, or that you will require for the application to work properly.
For example, I'm adding the camera permission here at this point.
So this is the absolute basis, otherwise the rest of the court will not work properly.
So it's really important that you set that up in your manifest.
So let's first just ask for the camera permission, and therefore I will require something called a
activity result launcher.
So I'm going to create a new global variable, which I'm going to call camera result launcher, and
it will be of type activity result launcher.
So you see you need to parse what kind of launcher it will be.
Well, will be passing test by a string.
So now we can define this here directly, and I'm going to register for an activity result like so and
here I can pass the activity result contracts.
Like so activity result contracts, plural, where I then request for permission.
And here at this point, I can now say whether it's granted or not.
So here we're passing the register for activity result, which will return an activity result launcher.
So we can now say if it's granted, so is granted.
Well, based on what we get there, we can now work with it.
And by the way, this is a little confusing here.
We need to make sure that we have the closing bracket up there.
OK, so we're calling this register for activity result and we are passing the activity result contracts
request permission method, which we then use a lambda here where we know whether the permission was
granted or not.
So this is just a form which you need to follow here.
All right.
So if it's granted, I cannot go ahead and check.
So if granted or is granted the precise here, then I can go ahead and display those.
So I'm just going to display this toast saying, Hey.
Permission granted?
OK, very simple.
Let's import toast here as well.
OK.
Permission was granted, and otherwise I know that permission wasn't granted.
So here, permission denied for camera.
OK, so this is our camera result launcher, this is how it works for one particular permission that
you can ask for.
And now inside of our own create, we actually need to launch this thing right in order to launch the
thing.
What I'm going to require is going to be my button.
OK, so here in my activity may smell.
I have this button with an ID of button camera permission.
So let me find it by outside.
I'm not going to use vue binding here.
I'm going to keep it super simple.
So here will button camera permission will be a button.
And I will find it by its side.
Like so and I thought I'd start button camera permission, so this will be a photo button, so I need
to import button.
So now I have the button and I can use the button camera permission.
Set on.
Click Listen there here.
In order to say what should happen once we click on the button.
So what I'm going to do is I'm going to check for the version that we are running here because if I'm
using an older Android version, I want to treat this thing differently.
So here, version SDK underscore it.
So I'm checking for the Android version that somebody is using.
With their phone.
And you're comparing it to be built short version coats and coats thought.
So if you're using Android M or newer, then run the code that I'm going to run because I'm not going
to run this permission code for all the versions that you would need to run different kinds of request
code.
But usually this would be fine.
So we're just double checking whether we're really using a phone that has at least Android.
And I think Android and was something like 4.1 or whatever, like super old version and here.
We also need to check if should show request permission, Russia, Neal, is.
True for our particular manifest that we're more manifest of permission, that we're looking at the
camera.
OK, so here permission and you see manifest was fine, but permission is not fine.
And that has to do with the permission that was automatically added being the wrong permission import.
So by default, Android Studio thinks it's a good idea to add this manifest.
But actually, we need to use the other one.
So delete at entry and go to import and then select the Android version here.
So you will see that now it's going to be this Android manifest.
So import Android Manifest and now we can actually do a check here.
Or actually run the code that we want to run.
So I'm checking whether I want or what I should show.
Request permission rationale, which means.
That if, for example, the application does not have access to the permission, then it should show
why it should use this application, and I'm going to create a little alert dialog here, and this alert
dialog will basically just display that I want to have access to a permission or the application won't
work properly.
So this will just be a method that we're using here, that we create where we have a title that we pass
and the message that we pass.
And then we create an alert dialog using a builder and this builder will reset the title, the message,
as well as the positive button, in this case, just a cancel button.
And what should happen once this button is clicked and we're just saying this miss should be executed.
So we're just saying, OK, if the user presses on this cancel button, then this Mr. dialog and of
course, we need to create this dialogue as well.
Let's show it.
So that's what this so rational dialogue method will do for us.
So here what I'm just going to say is that I would like to show the Russia-NATO dialog with a little
title as well as a message.
So let me put that in here.
So the the title should be permission demo request, camera access, and the message should be camera
cannot be used because camera access is denied.
So this is something that I want to display to the user as a little dialogue if I'm trying to access
the camera feature and it just doesn't work.
OK, so now if that's not the case?
And I should not show the rationale, then I'm going to launch for the camera result.
So camera result, launcher launch with the permission that I'm looking at so many first thought permission
dot camera.
OK, so at this point, we can test our application because in the uncreated, now we have this button,
once we click on the button, we request for permission for the camera permission in particular.
So let's run the application and click on the button.
You see the permission request comes up and let's not allow it.
And you see permission denied for camera.
Now let us request for camera permission.
You see permission demo requires camera access.
Camera cannot be used because camera access denied.
OK, so basically, this condition here is met, which is why the rationale dialogue is being displayed
because we don't have access.
So you were just checking that we have not access, so to speak, and if we don't not have access,
so to speak, or we weren't denied before, then we run into this block where we say, OK, then please
ask for permission.
OK, so this is where we're doing it.
And then accordingly, you would, of course, launched a camera then if you have the right to use the
application.
OK, so here camera result launcher, which is this camera result launcher that we created.
So now unfortunately, I will not have access to the camera permission.
So how can I manage dead while I can go to my settings?
And let me just go to the app settings?
Here, settings and search for permission, and then you can go for camera permission controller and
search for the permission demo here and allow only by using the app, for example.
OK, so now if I go to my permission request, you see permission granted for camera.
So now I granted it outside of the application.
Of course, I could have alternatively just deleted this permission and let me see if that's the right
one.
I'm going to delete both.
So the one that I had prepared, as well as the one we just built.
So here app info.
Uninstall and then reinstall by rerunning.
I now request for permission.
And now this time we are going to allow it.
Permission granted for camera.
OK, so you see this, are you contested?
But basically at this point, I can use the camera functionality of Android and use it in order to,
well, get an activity for result and get the image back that the user has to take in, for example,
with the camera.
So that's how we can ask for one particular permission.
And just quickly about this permission rationale here, the system automatically checks if the permission
exists, right?
And if that's has been denied, this would be activated to show the educational message about your app
explaining why the permission is needed, and I recommend it as the right way to check for permission.
So if permission has not been requested before, then this code here will be executed.
OK, so now let's see how we can ask for multiple permissions.
Therefore, we need to make a change to this part here.
Actually, let me reduce it to the smaller part here and just duplicate it.
And instead, I'm going to use camera and location result launcher.
So this will also be an activity result launcher.
But this time it's not going to just have a string here, but an array of strings.
So array of string and then accordingly, we need to make a change here as well because we are still
registering for activity result, but we're not requesting for one permission.
But we create we request for multiple permissions multiple and I need to close here with another arrow.
I believe.
Like so.
And according to get rid of this arrow.
So here we can now request for multiple permissions like so.
But now things are changing, so we are asking for multiple permissions, and we're not we don't get
is granted now.
But what we do get is if we look at it, we get a mutable map.
So it will be the mutable map now and we can get give it a name.
And I'm going to call it permissions.
OK, so based on the permissions that I am given now, I want to go through all of them with a for each
loop.
So here with this for each, I'm going to do it is well.
Here I get another immutable map with a string and Boolean value, which is basically going to be while
the string will be the key and the Boolean will be whether we have access or not.
So the vault permission name will be it.
But key so this it is this mutable map with a mutable entry.
OK, so this will be the permission name and then we have this is granted, which will be its value.
So these two values here, these two variables will now contain on one hand, what is the permission
that we requested for, for example, the camera permission and this one will hold the value, whether
we were granted this permission or not.
So now we can check if is granted is true, then we check for the permission name because we don't know
which permission was granted because it could be that we have the permission granted for the camera,
but not for the location or for the location, but not for the camera or for both of them or for neither.
So we have all of these different options.
Options are limitless, so we can now go ahead and use the permission we can check if we have access
to the final location.
So we find location well because I also want to have the location of the user, right?
And as I said earlier, if you want to get access to the location, you need to add to send what you
need to add this permission in the Android Manifest file as well.
So here I'm going to get access for fine location.
I'm going to search for that.
And then while you are using locations, you should also access the course location.
So at this into your manifest as well, this is just something about locations, because if you don't
get the exact location, you may be at least get a rough location, which will give you the exact course
location.
OK, so we're checking if the permission that was granted is the access fine location.
If that's the case, then I'm just going to display a toast saying, hey, the permission was granted
for the location and otherwise.
So if it wasn't the permission that was to access fine location here, of course, I could also use
an if statement and ask for the permission named specifically.
Again, this would make sense if you're asking for multiple permissions here, and in this case, I
do know that it will be the camera that was granted.
OK, so it wasn't the access fine location that was granted.
It will be the camera that was granted.
Even though we have to be careful here, I will say what?
Why in the seconds.
But generally speaking, this is how it would work.
So that's how it works for our if the permission name was to access fine location.
So then if we are in the last block here and it's fine to open it, but I think it's more readable.
So here we are.
Inside of this block off is granted.
So here, if it was not granted, then we know that the permission that we asked for was not granted
and here we need to do the same check.
So we check again for the fine location.
And we know that we were denied the location as well as denied the camera.
So in the health block?
OK, so this is a for each loop, right?
It goes through all the permissions that were granted or not granted.
And but depending on whether it was granted, it will run into this if statement and if it was not granted,
then we'll run into this statement.
So it will run into this block with all of its beautiful statements were, in our case, just posts
that we're going to use here.
OK, so now, according to him, I will need to make a change to my aunt, click listener here.
So instead of just using camera result launcher and launch it, I'm going to use my camera and location
result launcher.
And here I need to pass an array of permissions and not just one permission, so I can just go ahead
and create an array of the permissions that I want to use.
In my case, it will be the permission camera.
And that's like this came up.
Well, I need to write it manually, I guess.
And then separate it with a comma.
I add the second permission that I want to add here.
So manifest dot permission, dot and excess fine location.
OK, so these are going to be the two permissions that I'm asking for.
You could, of course, add another one here and so forth, but I'm just going to go with two.
OK.
So at this point, we should be able to run it and ask for multiple permissions.
But before I do that, let me uninstall the application real quick because I want to show you how it
looks from scratch.
OK, so now let's run the application.
And request for permission to see it asks for the camera, so I'm going to say, OK, I'm going to give
you the permission and then I'm going to give the precise permission while using the app and you see
permission granted for location and then just a second later.
Permission granted for camera.
Now, if I run it again, it will just display those permission details.
So now we know we have permission and we can go ahead and use those permissions in our application.
So now the thing is that I could also run into this weird situation.
I me show you where I uninstalled this application real quick uninstall and then run the application
again, because here I'm accessing the fine location or checking for the find location.
But what if I was given the course location and not the final location and the coarse location I will
show you is actually this part here.
This approximate, this is the course location.
So this precise here's the final location and this one will be the course location.
So let's select the approximate here.
And then you say while using and you see permission denied for location.
Even though we are well, we have access to the location, but not to define location, so we could
be more precise.
You're saying permission denied for fine location?
OK.
This LS, if could be the well, maybe a little challenge for you post the video and adjust this f block
here for the coarse location as well.
OK, so I hope you tried it, and I'm going to do it is I'm going to add the cause location here, the
permission and this one will be denied for.
Course location.
And the ALS blog, it will just be the camera.
OK.
So here, camera.
And then accordingly, we will need that.
And the top part as well here is if blog actually.
For our.
Granted, location, so here.
I'm just going to put it in here.
Permission, access, course, location, if that was the case, then permission granted for course,
location and accordingly, of course, this health block here.
Will be given like, so OK.
That's how you would solve all three options, but that's pretty much it accordingly, of course.
Yeah, you could.
How you should adhere to course location as well.
So here many first permission.
But, of course, location.
And now I actually have never tried it this way.
Let me try it.
If I will, I will get asked for permission twice to run it and see.
Well, like three times, actually.
So ask for permission, allow an hour precise allow.
And that's fine.
Yeah.
It still works.
So it gave me the permission granted for the final location, but not for the course location.
OK, cool.
That's it for this video now you have an idea of how to use permissions.
See you in the next one.