Hire everyone in this story I want to add another functionality to this application.
So this functionality is actually going to allow the users to be notified whenever they use it actually
changes his or her location.
Actually the physical location of the Android device.
Right.
So the user does not have to click on the button.
Get that data button in order to actually get there.
For example the time that is remains to get to the destination location or the distance that is actually
made through this to get to the destination location.
I want the app to automatically send this data to the user.
They use it to be notified automatically about these data up.
You are going to actually update our text views automatically.
So how we can do that.
I want to show you that here.
First of all we need to add some more codes here inside our main activity class.
So lets do that now.
First of all we need to go into the unconnected method.
So let's actually find the Smurfette let's search for this method here.
So if you are using a Mac you just need to hold down the command on your keyboard and then press and
then underneath that if you and your keyboard and if you are using a Windows machine you just need to
hold down the control on your keyboard and then press and delete that if on your keyboard.
So let's search for the unconnected method right.
So here it is are unconnected my faith and now here we are just actually outputting some values to the
log.
So now after this line of code as you can see I actually commented out this method here.
Right.
We are not going to use this method anymore.
Here it is and if it is used in order to get the users current location.
So after this line of code I am going to get the diffuse location provide that API.
So first this actually refers to this fuse location provide that API interface and I need to actually
give a name to this variable faced effused location provide that API assignment operator because this
is actually an interface we can not use the new keyboard in order to create an instance of a fuse location.
Provider API.
So here I'm going to refer to the location services here.
Location Services dot dot fuse location provide the API semicolon.
So in this way we are initializing our variable which is of type fuse location pro-white API.
Right now I can't say because we want to actually get the users.
Actually I want to get to the destination that it's remained to the destination to the destination location
or the time that is left to get to the destination location automatically Vinney to actually create
a loop location request object in order to do that job automatically for us.
So after this line of code here in line number 161 I am going to say location request.
So this is the type of the object that I am going to create right.
So the name of a variable is going to be location request assignment operator.
You can see a new location request semicolon.
So now we have an object of application requests that can request the location so that we can actually
specify the time that is actually left to get to the destination location or the distance that is left
to get to the destination location.
So we must actually create a location or request for that.
So after this line of code I can't say so first of all we need to specify the interval of the location
request so I can say location request that set interval and here I can say ten thousand right as you
can see it is of the milliseconds right.
What does this mean.
This means that 10000 means 10 seconds.
So after 10 seconds it the location eques is going to be he's going to actually request the location.
So this means that every 10 seconds the location is going to be a request that right.
If you want to you can actually change this value if you want.
For example after 50 seconds we can change the value if you want.
For example after five seconds you can change the value to 5000.
But I want that after this request to be executed after 10 seconds.
OK.
So after this line of code I can't say location request that sets priority.
So let's see what values we can use here.
So here I can't say for example just they've been here priority right.
And now look at these values here.
Let me just add to the whole vault here.
So you can see that the first one is priority balanced power.
Look at this one here.
It is used it is it is not going to actually use its battery a lot.
OK it is just going to actually for example just it's the use of a battery a little bit so that it is
its battery is not going to be drained.
And the I mean by you by saying use of Especially I mean the users the device back to you OK.
So their first option is this one priority balance power.
So if you choose this one the user's device battery is not going to be drained dramatically.
It is just it's just going to actually it a little bit of the battery power so that it's going to get
the location of the user.
And the second option is high accuracy this option is used if you want to get the highest accuracy location.
So if you want to use this option this option is going to actually eat a lot of battery.
OK.
The first option is low power.
So it is going to actually just eat a lot do a little bit actually of the user's device past the battery.
And the final option is this one.
No power.
So it is going to give you the worst location possible.
It is not going to give you a very high accurate location because it is not going to eat a lot of battery
power.
So for this application if you're going to use actually to balance out power so that the first value
is that the default the default value is actually this one balance priority balanced power if you want
to you can choose this one.
But the default value is this one.
You don't have to explicitly specify that's why you're here.
Right so I'm just going to put it here for you.
And the first line of code that I am going to say here is that location requires thought displacements
to just type in here.
It set smallest displacement or kit.
We have a getter and the center which as this one said smallest displacement.
And here I am going to assign the value 5 here.
So what is this.
What is this smallest displacement here.
After each for example after 5 meters for example the user has changed its device's location and after
five meters I want to execute it for example get the request I want to actually send a request in order
to get the location of the user's device.
So every five meters the request is going to be actually executed.
So this is disvalue is that if you want to you can for example change the value to 200.
For example after each 200 200 meters the location is going to be requested.
So this is for that.
And now I can say if Google API client will if a claim that is connected.
If it is actually connected actually.
So we created an if statement and he said the curly braces of this statement I can't say fuse location
provide that API that request location location updates to this method.
And now as you can see we need to provide free arguments for this method.
The first one is the Google client so we can actually say Google if a client we have available for that.
The second argument is the location requests so we already created a location request here which is
prime.
Yet with these arguments right for example the entire file ten thousand the priority priority balance
the power accuracy and the smallest displacement displacement five meters.
Right.
So we need to pass the location request here and now we need to specify the first argument.
So let me actually right this minute they get in in order to show you the argument here again.
So the first one is Google a client.
The second one is location request and the first one you can see is the location listener.
So what is this Vieni to provide a location listener here right.
Otherwise you are not going to be actually allowed to notify the application about the location changes.
So lets do that now.
So now you may ask that so what is going to be the listener the listener is going to be the main activity
class the main activity class is going to be the listener to the changes of the actually places of the
users device.
OK but here we have an error.
If I hover over this area it says cannot resolve Mefford request location updates and you can see that
it says that if we passed an invalid context here we passed the main activity that this is main activity
here is not listening to the changes of the users actually the device location.
So really to allow this class main activity to listen to the location changes how we can do that we
need to actually say that this main If the class is conforming or implementing the location listener
interface.
So before that we need to actually scroll to the top.
And now as you can see we are we are already implementing some interfaces here.
And the final interface here is our view that uncock us and lets put a comma here and now we need to
also actually conform to another interface meaning to implement another interface which is called location
listener to this one and then present it to the keyboard.
So now that we are actually conforming or implementing to the location listener interface we must implement
the location change method that is inside this interface.
So now let me actually scroll down here after the end in curly brace of this uncreate Smurfette here
I can actually say on location changed right and press and President to change in order to select the
Smurfette.
So now we have this method right now we are a hundred and fifty percent sure that diskless main activity
is actually listening to the last location changes.
If I scroll down here lets actually find the uncorrect unconnected Meffert you can see that there is
no error here right anymore.
So now after this line of code here we can see that we are actually requesting the locations for us
we are passing the Google IPO client and then we are putting the location request with these parameters
and then we are saying that the listener is diskless main activity class this context right.
So what happens if the application is not actually connected.
In that case we are going to try to connect.
So after the end in curly brace of this if statement I cant say else here I cant say Google for a client
that can act semicolon.
So if you are going to try again.
Right.
We need to be connected to the Google Play services in order to actually access the location changes.
So now that we have this line of code now its time to go to the location change method here.
So this is the method of the interface or location of the center.
So inside this method I can easily say on collidge So here I am referring to dismiss that uncle when
the button is pressed.
Right.
This Let me show you the design activity and then I mean that example.
So when this button is pressed the Anglican faith is going to be actually executed.
So because you want to get the location changes automatically without forcing the user to press on the
spot in I want the app to be able to get the location changes automatically without pressing this button
so let's go to them in the class and here I can say inside is on location change it.
I can say that this is going to be executed.
Unclick.
So I just refer to dismiss it Anklage.
And now you can see that if it needs an argument of type view.
So here because we don't have the view and we are 100 percent 150 percent sure that we have only one
button inside our view we are just going to pass on the knowledge here as an argument because we are
sure that this effort is going to be executed only when the user presses on the spot and get the data.
So let's go to that.
Let's go back to the and activity class.
So what happens when the app goes into background.
So let let me show the emulator here.
This is an application right.
And then I actually press on this.
I can hear the home I can hear if I click on it here.
Now you can see that.
Now the app goes into the background.
In this case in this situation all you want.
They're actually requesting the process of requesting the location to stop.
OK.
All I want only the app to request the locations when the app is actually on the foreground when the
app is running.
And the user is interacting with application.
I don't want the app to actually drain the battery power when the app is in the background.
Right.
So that is a notice that when we are in the background the unpause method of the application is going
to be caught.
So the unposed method is the best place to stop at the location or getting to process.
So now I can say after the curly brace of this on location changed method I can just step in and pause
right.
So after this line of code that actually caused the unpause on the superclass here I can't say.
So first of all we need to actually get the fuse location provide that API.
So just type in here fuse location provider API.
Let's give it a name assignment operator and here I can say location services that fuse location provide
that API semicolon.
And now I can say use the location provider API that remove location updates.
So we need to pass two arguments to the Smurfit the first one is Google API client come up and the second
argument is actually the location listener the listener is diskless like the context main activity that
this semicolon.
That's it.
Now again let me tell you that you must run this application on your actual device in order to see the
results because the emulator does not have the feature to get the user's location.
Or for example as specifying the distance or the time that is remains to that to get to the destination
location because of the current location is going to be not the emulator is not able to get the look
current location.
So when you must actually test this app on your actual device and thats it this is the end of this application
and you can actually extend this application and make it a very complex and full featured Android application
and upload it to the Google Peyster and other.
And I actually recommend you to do that because its a very powerful application.
It can be of very great application for your resume.
And if you want to get employed as an Android Developer its almost necessary for you to have Android
apps in Google place.
So I'm going to add another application to discourse about actually using the camera and taking picture.
So in the next application which is going to be app seventy seven you are going to create that application
in the next application is also going to be a production application so you can upload it right away
to the Google Base there.
So thank you very much for watching.
If you have any questions about the concepts that I taught you in this lesson please post the discussion
in the course and then I do my best to answer all of your questions.