Do onLocationChange get called by itself - android

I have one simple question. As Android Developer Documentation states:
You can control the frequency at which your listener receives updates
with the
second and third parameter—the second is the minimum time interval between
notifications and the third is the minimum change in distance between
notifications—setting both to zero requests location notifications as
frequently as possible.
But I have never seen onLocationChange called by itself. Does onLocationChange is ever called by itself. Either on time basis or distance basis. If yes how it can be replicated.
I have added this line in my code locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 200000, 100, locListener);
but location never changes by itself. Do I need to add some other things too.

You have to add that event.
`LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locListener = new LocationListener() {
public void onLocationChanged(Location location) {
try {
lat = location.getLatitude ();
lon = location.getLongitude ();
mapView.getController().setCenter(new GeoPoint(lat, lon));
//or something
}
catch (NullPointerException e){
lat = -1.0;
lon = -1.0;
}
}`
And to add it in some event (onCreate, onClick ect..)

Related

Location Updates

Iam developing an app that do some field signal strengths calculations every second by using Handler and during the Handler period it records the coordinates and record the results & coordinates. It works fine except when real testing and when I increase the speed of the vehicle the recorded coordinates not every second while some times every 2-3-4 seconds which is not accepted by me.
The Code below:
final LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
publicvoid method_01(LocationManager locationManager){
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Location locationDataRate = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double latitude = locationDataRate.getLatitude();
double longitude = locationDataRate.getLongitude();
x=y+1;
Save to file (latitude,longitude,x);
handler.postDelayed(this, 1000);
}
}, 1000);
}
I have tried also to change the time and min. distance to (1000,0)
First of all, if you want to receive an update every 1 second, you should not request more frequent updates. Therefore:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);
Anyway, the best practice for handling location updates is not persistently looping getLastKnownLocation but implementing the callback of location updates. There is a bundle of examples here.
To your question, in the doc they state that interval between updates in never guaranteed:
The location update interval can be controlled using the minTime
parameter. The elapsed time between location updates will never be
less than minTime, although it can be more depending on the Location
Provider implementation and the update interval requested by other
applications.

Request location updates not updating while driving

I have written this function for requesting location updates:
private void RequestLocations() {
locationListener = new LocationUpdater();
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,
100, locationListener);
} else {
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 100,
locationListener);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,
100, locationListener);
}
}
I am calling this function in the onStart() of a service which I have made a foreground service.
In locationUpdater I have implemented locationListener. In on location changed I am toasting lat/lang.
It is working fine if I am at a walking speed.
But while I am driving it is not as accurate and fast as it was with walking speed. Even though it is not toasting any location if I am at a speed of more than 50 KM/h and as soon as I stop it again starts working and toast a new location.
I wanted to make something in which as soon as I move 100 meters away it should toast new location whatever the speed is.
Please guide me and if I am wrong at any place make me correct.

How to search a GPS location in Android and have 2 different termination conditions?

I'm trying to find a way to turn off the GPS immidietly in case a good enough location was found, while still having a time limit to "give up".
I tried to do this with the following strategy:
start checking for locations, as soon as a location that has an accuracy lower than the maximum tolerated, pass it to the next function for processing and stop looking for updates.
Also, to save battery life, if such location could not be found in 30 seconds, stop looking for location updates without passing a value (basically give up, and hope to better luck next time).
To count the 30 seconds, I'm using a handler. But as soon as I write the line locationManager.removeUpdates(locationListener); in the handler, the locationListener in the parenteses in both lines (the one in the handler and the one in the listener) turns red and reports an error: The local variable locationListener may not have been initialized
Here is my code:
private void checkProximity() {
final LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
//start tracking location
final LocationListener locationListener = new LocationListener() {
...
#Override
public void onLocationChanged(Location location) {
//if new accuracy is better than the best estimate - update the best estimate
if(location.getAccuracy() < MAXIMUM_TOLERATED_ACCURACY) {
//forward location to scanProximity and end the location search
scanProximity(location);
locationManager.removeUpdates(locationListener); //FIRST LINE (see below)
}
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
500, 0, locationListener);
Handler h = new Handler();
int delay = 30 * SECOND;
Runnable removeListener = new Runnable() {
#Override
public void run() {
//if this code is reached - the maximum tolerated accuracy was not met in the period time
//extended to find a location
//TODO stop the location manager and return without forwarding a value
locationManager.removeUpdates(locationListener); //as soon as I write this line, the FIRST LINE and this line turns red.
}
};
h.postDelayed(removeListener, delay);
}
Is there anyway I can do this differently so I won't get an error?
I recommend you use Little Fluffy Location Library to work with GPS locations. Check out the examples codes and see which makes you more easy the solution to your problem , this is a beautiful way.

How to handle GPS under roof in Android

I have created application that gets a relevant lat and long and updates in a database. The problem is that when I under a roof like house or mall... I keep getting the GPS searching ICON on my app.
I am using a button to trigger the Mylocation() and it's listener.
The button has the following:
locationListener = new MyLocationListener();
location.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 5, locationListener);
and under the mylocationlistener method I have
private class MyLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location loc) {
try {
Thread.sleep(1000);
Toast.makeText(getBaseContext(),"Location: Lat:" + loc.getLatitude() + " Lng:" + loc.getLongitude(),Toast.LENGTH_SHORT).show();
location.removeUpdates(locationListener);
location = null;
finish();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
I get the value toasted properly without any problem and app is finished an GPS icon goes away but when I am inside the roof and GPS icon in top keeps searching for GPS. The icon doesn't go away.
How do I fix this? Do I need to put a condition if Gps signal is good start the GPS if not just message GPS signal not good. if so how?
Thanks!
As described in this question How to measure GPS signal strength on Android,
So assuming that you really mean signal strength, you can get the GpsStatus via LocationManager.getGpsStatus(), and that gives you a list of satellites via getSatellites(), and each one of those has a signal-to-noise ratio (getSnr())
You could consider using the network location service as well as or instead of GPS. You could also consider adding a timeout.

Android - Reliably getting the current location

My app checks at a specific time whether a user is at a given location. I use the alarm manager to start a service that makes this call:
locationManager.requestLocationUpdates(bestProvider, 0, 0, listener);
And also checks:
locationManager.getLastKnownLocation(bestProvider);
But I'm having problems when running on a real device. For one thing, getLastKnownLocation is most likely the last place the GPS was on, which could be anywhere (i.e., it could be miles from the user's current location). So I'll just wait for requestLocationUpdates callbacks, and if they aren't there within two minutes, remove the listener and give up, right?
Wrong, because if the user's location is already stable (i.e., they've used GPS recently and haven't moved) then my listener will never be called because the location doesn't change. But the GPS will run until my listener is removed, draining the battery...
What is the right way to get the current location without mistaking an old location for the current location? I don't mind waiting a few minutes.
EDIT: It's possible that I'm wrong about the listener not being called, it may just take a little longer than I thought... Hard to say. I'd appreciate a definitive answer still.
The code may be something like that:
public class MyLocation {
Timer timer1;
LocationManager lm;
public boolean getLocation(Context context)
{
lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
timer1=new Timer();
timer1.schedule(new GetLastLocation(), 20000);
return true;
}
LocationListener locationListenerGps = new LocationListener() {
public void onLocationChanged(Location location) {
timer1.cancel();
lm.removeUpdates(this);
//use location as it is the latest value
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
class GetLastLocation extends TimerTask {
#Override
public void run() {
lm.removeUpdates(locationListenerGps);
Location location=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
//use location as we have not received the new value from listener
}
}
}
We start the listener and wait for update for some time (20 seconds in my example). If we receive update during this time we use it. If we don't receive an update during this time we use getLastKnownLocation value and stop the listener.
You can see my complete code here What is the simplest and most robust way to get the user's current location on Android?
EDIT (by asker): This is most of the answer, but my final solution uses a Handler instead of a Timer.
If the user's location is already stable, then getLastKnownLocation will return the current location. I'd call getLastKnownLocation first, look at the timestamp (compare Location.getElapsedRealTimeNanos() with SystemClock.elapsedRealTimeNanos()) then register a listener if the fix is too old.

Categories

Resources