So first in our application let's go ahead and start off with the GPS and location code. >> All right, so one thing to note before we begin is the code that we're showing you here is essentially a template. So as you can see we already have a few text views that are tied into our views. We also have some of these methods that are already tied to our view here. So do note that this code here will be given to you, so if you wanna go ahead and follow along as we create this so you can kinda learn, feel free to get it from our GitHub. So the first thing we're going to do is we're gonna be dealing with this test location method. So with this test location method, you first want to create a location manager. So what we're gonna do is we're gonna call location manager. We'll call it, or we have to import that statement, or that class. We'll call it manager, and then we have to cast it as a manager, call getSystemService and get Context.LOCATION_SERVICE. >> So note here that he's calling to getSystemService. That returns a service object, or I forgot the exact name, but basically it's gonna be a general object which you need to cast to a location manager, or other manager, in order to use all the correct features. >> Before we delve further into the location manager, recall that in the previous section we did talk about a location listener. And so for us to be able to kind of pull information from the GPS or the location manager, we first need to instantiate a listener. So you can either define this listener in the same class or you can define it on an external class. Now personally for me, I prefer to define different classes in different methods, or different classes in different files. So we're gonna create a network test location listener class. >> And whatever Jacob's doing is actually considered really good programming because he's separating the different functionalities between different classes, so it's not in all one congested file. >> And so with this location listener class, do recall that it's an abstract class. So we're going to have to extend location listener. And doing so will give us this area because we haven't defined the methods, so we're going to use Android Studio to implement those. Oh, implements, not extends. Oops. And then we're going to implement the methods. So as you can see here, you have various different methods to use for the location listener. Now for our purposes we don't necessarily need to do anything with these methods, we just need it as a concrete class so that we can instantiate the object, so. >> If you are gonna be doing more GPS programming, you would want to go ahead and mess around with these methods. Such as the location change, will it give you real time updates when you have a new GPS coordinator or whatever. Or if your provider, for some reason, gets disabled, you wanna be able to catch that issue and prevent any more processing from going on. >> And so once we have this method, or this class defined, we can now go back to our test location and create the network test or the listener. So NetworkTestsLocationListener listener = new NetworkTestsLocationListener. So now we have our listener and we have our manager, and so what we want to do next is we want to do a request location update. So depending on which provider you want to use, you can go ahead and you can, recall you have the option to choose between GPS network and passive. For me, since for us, we want to get the most accurate location, what we're gonna do is were gonna first test to see if our network provider is available. So if manager.isProviderEnabled we're gonna do Locationmanager.NETWORK_PROVIDER. So if the network provider is available, we're gonna go ahead and request. Oh, whoops. It's manager.requestLocationUpdates and what we're going to pass in here is LocationManager.NETWORK_PROVIDER. Yes. We're going to pass in two parameters of zero. So these two zero parameters, the first one is going to be the minimum amount of time it would take for you to wait. And the other time is the distance that you wanted to kind of scan. So putting them both to zero will maximize the range and the requests that you would get. And lastly, we have to pass in the listener as our last, whoops, as our last parameter. So now once we have this, we're calling the manager and we're asking for a location update. We now want to get the last known, we want to pull in the information. So what you're going to do is you're going to do manager.getLastKnownLocation, and in this parameter your gonna pass in the provider. So we're gonna do LocationManager.NETWORK_PROVIDER. >> Now this method actually returns a location object so you probably want to set it equal to that location object. >> Yep. >> Oops, there you go. And we need to import the class, and yeah. So now that you have this, the location, what you can do is we can go get our status so LocationStatus.setText and then you can put in Your Ccoordinate is, put in that plus, location.getLongitude +, + Location.getLatitude. And then close that up. So what this will do now is, if your provider is enabled, it will first request for the location update and then pull the location and then set the location to the text. >> So I have a quick question, Jacob. Let's say the network provider is not enabled. What's going to happen? >> So if your network provider is not enabled, what this test will do is that it will skip it over and it will check, or what we want it to do, it will skip it over and perhaps check our other instance. Now, let's say neither of our GPS and network provider is going to be enabled. If that's the case, then what we want to then is we want to handle that with an error case by setting the status to say that your GPS provider is not currently available. >> Okay. >> Yeah, we will add that in after we do the GPS provider. >> Perfect. >> So if your network provider is enabled, you would go ahead and do the location stuff. Now what we want to do is if we have a more accurate. Now what we want to do is if we have a more accurate provider, which in this case is a GPS, if it is also available, we'd want to replace the text that we have for the network provider with a GPS provider. So in short, we're gonna be repeating this code, just gonna copy and paste that here. And instead of using the network provider, we will be switching over to the GPS provider. >> So usually copying code is actually usually a bad idea to do. So what might have been a better idea here to do here is go ahead and create a method that does that snippet of code and just pass in to the provider and just replace as you go. >> Yeah. >> I think for us for now, we're just going to go ahead and go through it just so we can get the code done. In the final iteration, I think we'll do some fit right three factoring and making it look pretty. >> So if neither of these codes work, we want to, oh before we do that, we wanna first put both of these cases in an if statement that would check to see if either of the provider is enabled. So we're gonna go ahead and do just that. So this is basically the big check to see if we have any sort of provider there. Add that back in here, do Enter and the extra bracket. So what this L statement is catching is that if neither of your network or your GPS provider is located, or is available, then we'd wanna say in the locationStatus we're gonna set the text to be, Your device does not support GPS/ Network Providers, not support GPS or network provider. There we go [LAUGH] yeah. So, that would be our test location. So, after writing the GPS and location aspect of the application, recall that we used the get system service, we casted it to a location manager, and we also created the network location listener class. Now these are all fundamentally necessary for you to be able to pull the location, and so with that in mind your test location aspect to the application should now be working.