File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Android and the fly likes is there any way to trigger onLocationChanged other than moving Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "is there any way to trigger onLocationChanged other than moving" Watch "is there any way to trigger onLocationChanged other than moving" New topic
Author

is there any way to trigger onLocationChanged other than moving

sam liyanage
Ranch Hand

Joined: Nov 25, 2008
Posts: 1031
I need to get current location longitudes and latitudes.But without moving to any location.So is there a way to trigger onLocationChanged method without changing location ?
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
You will only get updates if there *is* a new location. But you will get an initial "update" with the current location no matter what. If that's not sufficient for your purposes please explain in more detail what you're trying to do.
sam liyanage
Ranch Hand

Joined: Nov 25, 2008
Posts: 1031
But you will get an initial "update" with the current location no matter what


1)How it get currentlocation without changes.Can you show with example ?

2)When i run belo codes it alway goes to else part ,without any location changes.Can you tell me the way to go inside in if part without location changes.


Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
How it get currentlocation without changes.Can you show with example ?

Just register for location updates and you will receive at least one, even if you never move.

When i run belo codes it alway goes to else part ,without any location changes.Can you tell me the way to go inside in if part without location changes.

I've already told you in some other thread that there won't necessarily be a last known location. So any code that relies on there being one is doomed to fail.
sam liyanage
Ranch Hand

Joined: Nov 25, 2008
Posts: 1031
just register for location updates and you will receive at least one, even if you never move.


please look at below code



when i run this code it gives me nullpointer exception.If it always give a location how it gives null pointer.

sam liyanage
Ranch Hand

Joined: Nov 25, 2008
Posts: 1031
See the attachment for nullpointer


[Thumbnail for null.png]

Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
which object is null?

Update: I see, lastknownlocation - you already know that it *can* be null, so why are you working with it without checking whether it *is* null first?
sam liyanage
Ranch Hand

Joined: Nov 25, 2008
Posts: 1031
lastknownlocation is null.But provider is not null.See the attachment.


[Thumbnail for provider.png]

Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
The provider has nothing to do with it. lastknownlocation is null, then you call a method on it; consequently you get a NPE.
sam liyanage
Ranch Hand

Joined: Nov 25, 2008
Posts: 1031
1)what is the reason for this ?

2)I have buitin Google Map application in my phone.But it never gives me such a error.It always shoe my location.How they built it ?

3)If this happen there is no way to get rid of it.That means we unable to run my application.Isn't there anay solution ?
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
1)what is the reason for this ?

Not sure what you're asking - why you're getting an NPE if you invoke a method on a null object reference? That's how Java works; surely you have encountered that before.

2)I have buitin Google Map application in my phone.But it never gives me such a error.It always shoe my location.How they built it ?

Probably the same way you're trying to program yours. They're just not invoking methods on null object references.

3)If this happen there is no way to get rid of it.That means we unable to run my application.Isn't there anay solution ?

Of course there's a way to solve this. Why do you think there isn't? Why do you not want to check for null before using an object reference?
sam liyanage
Ranch Hand

Joined: Nov 25, 2008
Posts: 1031
Of course there's a way to solve this. Why do you think there isn't? Why do you not want to check for null before using an object reference?


it is ok.But what must do in else part.

other thing is if i use criteria.setAccuracy(Criteria.ACCURACY_COARSE); it doesn't give me null pointer most of time.But if i use criteria.setAccuracy(Criteria.ACCURACY_FINE); most of time it gives me null pointer.Is there any connection with these things with null pointer ?
sam liyanage
Ranch Hand

Joined: Nov 25, 2008
Posts: 1031
what is the different between (Criteria.ACCURACY_COARSE) ,(Criteria.ACCURACY_FINE) ?
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Let's be very clear about one thing: "it" does not give an NPE. Your code -which is improperly not checking an object reference for null before using it- is reponsible, nobody else is. If you had wanted, you could have gotten rid of the problem long ago, by not accessing lastknownlocation if it is null.

I'm not sure what you mean by "else part", I don't see an if statement in the code you last posted. The underlying problem is very simple: there may not always be a last known location. Specifically, when the app is just starting up, there almost certainly won't. All your code needs to do is to check for that, and act accordingly. For example, by not zooming in on a position. You could show a map of New York instead, just so the user has something to look at.
sam liyanage
Ranch Hand

Joined: Nov 25, 2008
Posts: 1031
I need to store last location longitudes & latitudes in my application.So if the lastknowslocation is null i get last location visited and show it.So how to store last visited longitudes & latitudes.
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
So how to store last visited longitudes & latitudes.

Shared Preferences: http://developer.android.com/guide/topics/data/data-storage.html#pref

But even then, the very first time the app runs, you will need to decide what to do if there is no last known location. You can't escape this fundamental problem.
sam liyanage
Ranch Hand

Joined: Nov 25, 2008
Posts: 1031
I think this way.
I found that we can get location from cell id also.Then first i search for location from GPS.if lastknown location is null then i try with cell id.So then i amable to get a location anyway.Is it correct ?
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
I very much doubt that a cell/network location will always be available when your app is started. And the user may have that turned off to begin with. Do you really want to require an additional permission on something this uncertain?

It seems that you want to fight tooth and nails the notion that there may not be any location available, while in practice it's all too easy to imagine that just that will be the case. Do you really want your app to fail in those circumstances? Why?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: is there any way to trigger onLocationChanged other than moving
 
Similar Threads
prevent double submission
scholership
Audit Logging in Ibatis.
it doesn't give me the country name
How to trigger a mouse event with out MouseListener