I have a class implementing the LocationListener
public class GetLocation implements LocationListener {
#Override
public void onLocationChanged(Location location) {
Log.i("GetLocation", "Location changed: " + location.getLatitude()
+ ", " + location.getLongitude());
}
In my activity,
final LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
final GetLocation locationListener = new GetLocation();
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, locationListener);
..... //Check whether the location is changed
locationManager.removeUpdates(locationListener);
updateGPStoServer();
After onLocationChanged() is called once, I want to do a upload task and cancel the listener, so I am asking how can I wait for the onLocationChanged() then do my tasks?
After onLocationChanged() is called once, I want to do a upload task
and cancel the listener, so I am asking how can I wait for the
onLocationChanged() then do my tasks?
I think you need to move the call to updateGPStoServer() into your Listener.onLocationChanged method; that would implement the "waiting" that you're looking for. (On a separate note: updateGPStoServer() should be implemented to create a background Thread to do the updates to the server. But you knew that, right? :-)
Also, it sounds like you really want to be calling LocationManager.requestSingleUpdate instead of LocationManager.requestLocationUpdates. That would remove the need to call locationManager.removeUpdates(locationListener).
Related
OLD QUESTION:
I'm trying to get my device's location coordinates and I've followed all the steps that I've found in multiple areas while researching. I've set up a LocationManager and used the requestLocationUpdates function that is tied to a LocationListener. However, the LocationListener does not respond. I've tried debugging as well as walking around outside in order for the onChangedLocation function to execute but nothing happens. In debugging the requestLocationUpdates function for my LocationManager is executed but the LocationListener is never executed.
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
textView = (TextView) findViewById(R.id.textView);
textView2 = (TextView) findViewById(R.id.textView2);
locationListener = new myLocationListener();
textView.setText("Longitude", TextView.BufferType.NORMAL);
textView2.setText("Latitude", TextView.BufferType.NORMAL);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 0, locationListener);
requestLocationUpdates
Above is the use of the requestLocationUpdates function.
private class myLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location location) {
//Log.e("Latitude: ", "" + location.getLatitude());
//Log.e("Longitude: ", "" + location.getLongitude());
if(location != null)
{
textView.setText(Double.toString(location.getLongitude()), TextView.BufferType.NORMAL);
textView2.setText(Double.toString(location.getLatitude()), TextView.BufferType.NORMAL);
}
else
{
textView.setText("No Location", TextView.BufferType.NORMAL);
textView2.setText("No Location", TextView.BufferType.NORMAL);
}
Toast.makeText(getApplicationContext(),"onLocationChanged Success",Toast.LENGTH_LONG).show();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
}
myLocationListener
This is myLocationListener that implements LocationListener. I've added a little bit of extra code for testing purposes. The toast would never pop up so it appears as though this code is never executed. If anyone could help me out with this I would really appreciate it.
Thank you!
NEW QUESTION:
After continuing on developing in this page while waiting for a response I noticed that it takes about a minute for the location services to actually begin working. So, now my question is: how do I overcome the obstacle of a user having to wait to use the app? I've seen apps that use location based content and it does not take that long. I know that there is the getLastKnownLocation function but what if a user travels 50 miles before opening the app again? Any help on this would be appreciated. Thank you!
Each device which makes location request for gps, has to wait until gps hardware become warm. The wait time changes by device and where you stay. If you are inside a building, this time could take 1 minute or more.
To avoid wait, you can use getLastKnownLocation method, if returns a cached location, check location's date via getTime method. Determine yourself, is it old location by your scenario ?
if it's too old location, you have to make location request and wait.
I have following code:
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
and MyLocationListener class:
public class MyLocationListener implements LocationListener{
#Override
public void onLocationChanged(Location loc){
loc.getLatitude();
loc.getLongitude();
tv_GPSlat.setText("Latitude: " + loc.getLatitude());
tv_GPSlon.setText("Longitude: " + loc.getLongitude());
}
#Override
public void onProviderDisabled(String provider){
Toast.makeText( getApplicationContext(),"GPS is not working", Toast.LENGTH_SHORT ).show();
}
#Override
public void onProviderEnabled(String provider){
Toast.makeText( getApplicationContext(),"GPS is working",Toast.LENGTH_SHORT).show();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras){
}
}
I would like to save current longitude and latitude to my TextViews (tv_GPSlat, tv_GPSlon) but the location values are not constant (they are changing all the time). How can I do this?
GPS isn't exact- even if you don't move it will bounce around a bit. Just put up the first location you get, and ignore future updates unless they move by more than a certain amount. That's the easiest way to do it.
You have to get the location and once you get it (i.e. your handler method is invoked) you have to unregister the handler in order to stop receiving the updates. Simply add this line at the end of your handler method onLocationChanged() in MyLocationListener:
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mlocManager.removeUpdates(this);
Add a data member to your location listener, and keep the previous location in it:
public class MyLocationListener implements LocationListener {
public Location mSavedLocation;
#Override
public void onLocationChanged(Location loc) {
// If we don't have saved location, or the distance between
// the saved location and the new location is bigger than
// 5 meters (~15ft) save the new location
if ((mSavedLocation == null) ||
(loc.distanceTo(mSavedLocation) > 5)) {
mSavedLocation = loc;
}
// Update the screen with the current saved location
tv_GPSlat.setText("Latitude: " + mSavedLocation.getLatitude());
tv_GPSlon.setText("Longitude: " + mSavedLocation.getLongitude());
}
// ... no changes to the rest of the class
}
Now the rest of your code can also get the latest saved location using:
mlocListener.mSavedLocation
I wonder why no one mentioned this. May be I a missing something. The call you have made has 0,0. It should have milliseconds, distanceinmeters. This way location change is only called when a particular distance is traveled OR after a time out. I am using both GPS and NETWORK providers to not be dependent on either too (sometimes GPS is not reliable).
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, God.KM20TIME,
God.KM20DISTANCE, (LocationListener) updates);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, God.KM20TIME,
God.KM20DISTANCE, (LocationListener) updates);
Can I use the same locationManager for requesting updates for the gps and network provider, or should I create two locationManagers and separate onLocation changed functions etc. I got really confused about this
You will want to check and see which one is available in the order you want to use them and then use the corresponding one:
if(mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOCATION_UPDATE_MIN_TIME, LOCATION_UPDATE_MIN_DISTANCE, locationListener);
}
else if(mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, LOCATION_UPDATE_MIN_TIME, LOCATION_UPDATE_MIN_DISTANCE, locationListener);
}
You can use the same listener because all the listener's methods take as a parameter a Location object which is source agnostic:
#Override
public void onLocationChanged(Location location) {
// updateLocation();
}
i'm working on app, which must get latitude and longitude. in my case requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener) is not in option. why? because i must get location just when user do something that location is needed (let's say he hits button). and i need location exactly on the time, when button is hit. in requestLocationUpdates, we can set minTime-if i set this let's say on 30000 the location at the "button hit time" won't be as good as i want. of the same reason minDistance is also not as good as i want. if i understant function requestLocationUpdates correct-when minTime and minDistance are set to 0, location is updating all the time. please correct me if i'm wrong. app is for company that i'm working at and that app will be used through the day and night. so if app will check for location updates all the time, battery would be often empty. that's why i need location just at the time, that button is hit. users are not having access to internet and gps. so i must use NETWORK_PROVIDER
I also have problems with getting location after reboot device. it doesnt work until i run google maps. users wont have avalible this either, so what function do google maps on android use to get location after reboot?
at the moment i use this code:
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener);
mlocManager.removeUpdates(mlocListener);
public class MyLocationListener implements LocationListener{
#Override
public void onLocationChanged(Location loc){
latitude=loc.getLatitude();
longitude=loc.getLongitude();
Text = "My current location is: \n" + "Latitud = " + loc.getLatitude() + "\nLongitud = " + loc.getLongitude();
Toast.makeText( getApplicationContext(),Text,Toast.LENGTH_SHORT).show();
}
#Override
public void onProviderDisabled(String provider){}
#Override
public void onProviderEnabled(String provider){}
#Override
public void onStatusChanged(String provider, int status, Bundle extras){}
}
code is from here
but this code is not working right. if i press "get location button" the Toast is shown and location is correct. but then if i press button several times, it does nothing. -but after 5 minutes Toast is shown like hundred times... why is that?
You are immediately calling removeUpdates after the requestLocationUpdates. So, the listener is never registered. Call the removeUpdates in the onLocationChanged method.
How do I unsubscribe a LocationListener from recieving updates from the LocationManager?
Here is how I'm setting it up
mLocationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE);
mListener = new LocationListener() {
public void onLocationChanged(Location location) {
Log.i("LocationListener", "Logging Change");
}
}
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
5000, 1, mListener);
After I have exited the the view that created the LocationListener I am still getting log messages in the LogCat window.
I understand that this is because I am orphaning the listener but I cannot see any destroy method on the LocationListener nor can I see any "remove listener" style methods on the LocationManager object.
Call removeUpdates on LocationManager, passing your location listener.
mLocationManager.removeUpdates(mListener);
I think removeUpdates should help.
mLocationManager.removeUpdates(mListener)