Skip to main content

Displaying the Current Location on the Task List

Lesson 33 from: Developing Android Apps with Java

Tony Hillerson

Displaying the Current Location on the Task List

Lesson 33 from: Developing Android Apps with Java

Tony Hillerson

buy this class

$00

$00
Sale Ends Soon!

starting under

$13/month*

Unlock this classplus 2200+ more >

Lesson Info

33. Displaying the Current Location on the Task List

Lessons

Class Trailer

Day 1

1

Overview: What is Android?

19:56
2

Eclipse and SDK setup and build a Red Flashlight app

38:05
3

Ad a button to a Green Flashlight

21:06
4

Add the Green Flashlight

18:48

Day 2

5

Week 1 - Layout and Build a Task Manager App

04:10
6

Displaying a list of tasks

40:44

Lesson Info

Displaying the Current Location on the Task List

step for displaying the current location. That's what we're gonna be doing right now. So what we want to do is just for our purposes. So we can see as an audience Um, where the current devices location is. You probably want to have something that's a little more interesting to the user. Like looking up, Um Ah, like a street corner, a pair or something like that and displaying that. But for our purposes, it's enough to just show this lat long inaccuracy. Um, I put the actresses accuracy in here so you could take it out on your device and try it out. But, ah, on the emulator, the accuracy will always be zero meters, and it will always report that it doesn't have an accuracy. Um, So, um, that's what you see at the top here. Um, but for our purposes, it's enough to show that lat long and just just to show that we're actually adding location. And, um, to do that, we will use the location manner manager, which is the lower level AP I than the my location overlay. And it's actually probably w...

ith the my location overlay uses. Um, that will update. Um the ah device. And when the devices location, when he first virus fired up, they will get the devices location and then update whenever it changes, as long as we choose to listen for updates, Um, and again t conserve power. We will shut that off on on pause and started up on on resume. And then we'll just, um when the new location arrives, will display what the location is that we could just use that Ah, send to give it a location that would be good enough for now and then. Ah, we will know the devices location after that. So the first steps are to set up the main XML, which is in resource is layout main. To have a text of you up at the top, get rid of this properties guy, switch over to the XML view. I'm going to add another text view up above the tasks. Title lips are his text title now, as is relatively, that's right. So, next to the Tasks title, I'm going to put location, text and just have it. Ah, a line parent. Right, So that will just sit to the right of the current title and that's enough for that. Save that and then open up the view tasks, activity and get a handle on the new view that we just added. We'll call it the Let me see what I call it and co. Just to make sure I'm coming in the same thing. Oh, co location, text. You will get that from fine view by I d are that I d dot location text. And then we'll add that hovering over here saying at a field the location texts. And now we've got a text. Okay, now we have the texts that we can put the location into. So what we need to do, um, to start listening to location updates, um, is construct or get an act, get access to the location manager. So let's do that. All here and on create and will create a method called set up location to do that. So I will hit command one on the Mac control one, I think, on the window on windows and then create that method. I think that method should be down at the bottom here with the rest of our set up, so I'm gonna move it down to the bottom of the file underneath, let's say above set up views and in here is where I will get that location manager up and running. So location manager. We don't actually said construct just a little bit ago, but it actually we don't construct one. We get access to what's called a system service. So let's call location Manager our field. And that is of type location, location manager, which is gonna have to be imported here in a second. Well, in any case, clips not updating that. So, um, once I set this up, maybe we will. It will think about it a little bit better, but like I said, it's a system service. So it's a service that's already running on the device or is already available on the device for you to get access to, um, something else, maybe using it at the time or not. But, um, there is only one of them, and that's why you have to get access to it instead of constructing one. Um, and you do that by calling get system service. You get it by a name, and there's a constant used to get access to that, and that is on context, location service. So this will get us in a ah ah, handle to the android dot location that location manager, which we will set up a zey field hands in after we have a reference we need to request location updates. So location manager request location updates, and there are a couple different versions of this method. Let's look at all of them. The different arguments request location updates. Um, first of all, you need to use a provider and we'll talk about that in just a second. But basically that is GPS or WiFi or ah, we could look at the different different ones. You can choose their. And then there is a minimum time that you want Teoh check for updates that you can use. So we'll use, um, 60 there in a second to be, um, at a little stand for seconds, actually, So that will be every minute. We want to make sure that we have, ah, the freshest location possible, so it will call back to us every minute. If there is any changes will check. It may be checking faster that, but it will only tell us that there's change every minute, so if we're the only Only only ah consumer of that service than that will keep it down to that level of activity. Then there's, ah, minimum distance that we will will give it. Teoh. Say, if we didn't move outside of this, um, this distance from the previous location, then don't give us an update. So then you can say we're not really interested in getting to, um, like we're only interested in, like within 100 meters. And if the if the new location that comes in isn't different from 100 meters, then don't worry about updating us, and then we use you can either use an intent or pen which called a pending intent, which is an intent that you can ship around Teoh, Um, maybe a background running service or something where you can use a looper, which is something we haven't really talked about. That's a little more advanced, but in our case, we just want to use a location listener. So let me set this up like this. So for a provider, let's use the GPS, and we'll also look at the other ones that you could possibly use and you get that is a constant from location manager. Here are the providers you can get network provider or GPS provider, and it didn't never provider is not going to show. Um, okay, here's the Here's probably the answer to the question. I'm looking down here. The documentation the network provider is I can't go back to get the documentation here GPS provider will look at in a second, but network provider. It's kind of tough to get these floating windows to work, but I'm gonna zoom in slowly on the documentation. And, um, it says here that this will get us either cell tower or WiFi, um, access points to give us a location. And that can still be a fairly good locations. That may be enough for your needs. And it looks like it requires either the access course, location or access. Find location, Permission. Um, so that says to me that fine location will give you, um, the course location, um, permissions as well, But, um, I guess that's not not completely clear still yet, but it seems to me that that's that's the case. So in any case, we want to say, use GPS, which will give us satellite fix or the finest location that we could get the most accurate location we could get. So that is the provider we want to use men. Time will say mid distance was just say five meters because we want to be really accurate. And then the listener will uses this, which is this class. But that doesn't That's not gonna quite work yet because we haven't fulfilled. Ah Thea requirements for the View Task Activity Act is the location, listeners. I can come here to one of these quick fixes an eclipse and say Let view task activity, implement location, listener. If I click that, then now this activity will implement location listener Um but it will not of have ah implemented the method yet so I can hover over this class and say Add unimplemented methods and it will add all of the methods down here in this gutter over on the side. It will show me some comments for the auto generated methods that it just added. It will add on location changed on provider, disabled on provider enabled and on status changed as the four different methods that we need to implement as a location listener. Um on provider, disabled and enabled will give you messages when the user shuts off or some other. For some other reason, one of the providers is turned on or off as a as a possible provided that we can use for location updates. So if the user goes to shut off GPS location, which I believe you could do seven separate from just shutting off location altogether, then we would get a message in here, um, that we can no longer get GPS updates, so we might want to fall back to you to network updates. But we've only asked for GPS. So we're only were saying that we only care about GPS in this case just for this simple example. Um, you could get more complex if you need, but for our purposes were not really interested. If the provider disabled or enabled messages come to us and on status changed, we might find out that, um, like we just, for instance, we just got kicked off of WiFi or something. We moved away from WiFi range, and now we're on cell tower range. If we were at using the course location, um, provider, then we were the network location provider. Then we might get a status change message there, which would let us know that something about the status of one of those providers change. Then we can decide what to do there for our purposes, though all we really care about is on location changed. And that's kind of the meat of it. Um, of all this location stuff, this will tell us when we now have a new location of of the device. So let's store. That is something called latest location. As a field on this activity. Create this field here. And, um, that location text that we set up a the top I'm going to paste in the code that will format the locations, latitude, longitude and accuracy into a string that will put into that location text so that we should see that showing up at the top of this screen. And I think that should be enough to run and see how things are working. And the emulator. It looks like we're getting location updates, or at least the emulator thinks we are, but we might actually have a location. This means we're requesting location updates. Sorry. Um, but we may not may still not have a location, because again, the emulator sometimes thinks that it doesn't have one. So I'll come back to d d. Mess here, Actually, let me do this. I'm going to minimize this footprint here. That's which already mass and then get the emulator sitting off to the side so that when I hit send, we should see it show up right away. Yep. Honor does. So it sees that we're looking at tasks, and we're at this location this latitude longitude with this accuracy. Again, the emulator always thinks that it has zero accuracy or no accuracy. But you should get something more interesting if you decide to put this on your device again To save power, we want to shut off location updates on pause and on resume. Start them up again. So right now, we already haven't on resume. Um, get the code for that pause. I want to shut that down. Ah, I am. See, we're in my view task activity. No, it looks like I didn't even do that and said my sample code. I need to update that as well. So on resume. We have this location manager handled here. Location manager the dots to get on the methods we have remove. I think it is. Remove updates for a certain listener. And that listener is This is what we want to do to shut it down and then to start listening again will do exactly what we did here. Where we, uh ah see, where was that? I s and set up location. We need request location updates again. Um, we just copy this in to your own resume. So this is what we'll do on resume. And then on pause, I'm going to go to source overriding implement methods so I could get the rates signature if I just click into this list. By the way, I We've seen this before. This all right implement methods list here. But if I start just typing Ah, looks like I have to have this expanded. I just start typing on pause. It should jump down to that. So that's a nice way to navigate through here. And then on pause is the one we want Teoh implement. So I'm going to shut off location updates on pause. Um, and then whenever so you don't actually shut down the location manager, you just say I don't want to have any more updates, and then you'll rely on the location manager Teoh to decide if there's no more. Um no more instances of other listeners asking for updates, and then it will spend down the devices GPS systems, if necessary. Um, OK, so this should be working. Let me just run this as a smoke test, and then I'll ask if there's any questions. So now when we navigate away here, I just had it with Dede. Must real quick. Senate location. Now we have a location. Goto add task Move away. That should have removed itself as a listener. And then when I come back, should add itself Is the listener never should be. Everything should be working fine there. So is there any questions about what we did there? Yes, I have few. Um, in a camera. Which method? It was we set up How? Um, often we wanted to listen and we said it to 60 seconds. There. There we are, right there. Um, is that is that guaranteed to give us at least one update every 60 seconds? One update every 60 seconds. Potentially. Mawr. Um, I don't think it's guaranteed, because once you, it, um I mean the power management profile of Androids, whatever. Whatever Androids is using Teoh, um, manage the power. Maybe Maybe it'll shut things down. I don't know how. I don't know if that's really a guarantee to call back with with the Well, let's look at the documentation here and see if they make any guarantees about anything. Got a hover back over here. Hit F two to focus this and this. Look through here. Um OK, so registers the current activity to be notified periodically by a provider. So periodically it will be updated with a new location. They may take a while to receive the most recent location because it may be spinning. Spinning up is a term people use. Sometimes it basically just means like sending up that GPS radio or the other providers system for getting the location. Um, but here's something interesting. You can also use get last known location string, which will you pass in a location provider type or the provider string like GPS. You could say I want to know the last known GPS location, but I don't know how much you want to trust that because that may be completely wrong for your purposes. Maybe it's OK to just show something. So the user kind of trust that something is going on, But But all the while, you know, kind of showing that you're updating location. Um, see, the case provider is disabled by the user. The updates will stop. So if the user, just like I talked about, goes into their settings and shuts off GPS, then you will get in on provider disabled, and then you'll get a provider enabled. If it gets turned back on. Here we go. The frequency of notification may be controlled using men time and distance. If men time is greater than zero, the manager could potentially rest for men. Time, milliseconds. That's milliseconds. Ah, so I need to up that if I really want a minute, um, so and so that's gonna be pretty fairly frequent. Requests its potential. Um, so let's see if there's any sort of contract here. Could potentially arrest for men Time, milliseconds between location updates, Conserve power in distance is greater than zero. Location will only be broadcast if the device moves by. Men distance meter to obtain notifications as frequently as possible. Said both to zero background services should be careful about sitting a sufficiently high time. So the devices against seemed too much power in particular. Values, values under 60,000 milliseconds. Air not recommended for background, um, services. So I guess, you know, I don't know how much of the guarantee it is that you'll be getting everything, actually any any updates, right on the the point that you want it. But, I mean, you're not really depending on that too much in your code. I can't imagine. So, um, whenever it calls back, you're gonna do something. And, uh, I don't know, design, answer the question, Or is there is there specific need that you can think of to get things on it? Really? Um, like, exact schedule. Yeah, I think it's just a general question about sort of how often that would actually happen. Um, okay, well, yeah. I mean, it should be relatively near this. Um, if the if the distance and the time hasn't changed, but it won't be. It won't just, like, decide not to give you something, because it'll unless that provider is disabled. Okay. And there's no way to get your laptop to provide a really location based on WiFi, is there? Um, not that I know of for updating the the emulator. There may be a way of tricking it if you're if you're operating system supports, you know, WiFi location or something like that. Ah, I don't know what that would be, though. The top gray line Tony is talking about is the current latitude longitude from the devices GPS and not the tasks GPS Could Tony clarify this? Ah, the latitude and longitude and the top gray line. Oh, right. Yeah. This This is the devices location. Not anything to do with the tasks location. Okay, if we keep location on, how often do we get a notification? Assuming we're moving, I think this has been answered. But I just wanted to make sure that that was the question I asked earlier. Okay, if you set the Yeah, but if you said everything zero, you'll be getting a pre frequently. And what happens if some other applications using GPS in the background? Um, nothing, really. I mean, I guess the GPS will be spun up in the radio, be working and consuming power, and in any locations that you ask from for? Ah, from the location manager in your application will come back pretty quick with a fairly accurate response. Okay? And And I had one more quick question before we go on. Um, is there a way to ask for the best available location data between the network and the GPS? Or do you have to program that yourself in the various on methods? Uh, I have the code that I had to right to do this for break. It was pretty specific. I wanted it to work a very specific way. So I figured that out myself. I think the reason you're asking that question is because IPhone can give you the best possible one rate. Oh, I I don't know. Somebody asked that question and they hadn't even Oh, okay, because you can ask for the best provider. I can't remember if there's a way to ask for the best provider here in the location manager. Let's let's take that up on the form, because I I may be confusing the two frameworks, but I think there may be a way to ask for what's the best provider possible. Okay, right. Which we take into account like what? The user has shut Shut down. You know, it won't take into account like, um, conditions or something like a fear here in a building that it won't say. Don't use GPS. It will still probably say use GPS as the best provider. So that's the kind of thing it'll bring back. But but let's talk about that on the forms when we can look back at the code.

Ratings and Reviews

a Creativelive Student
 

I thought everything on this site was free and now they want 99 dollars for a course

a Creativelive Student
 

very good

Student Work

RELATED ARTICLES

RELATED ARTICLES