Android same GPS location repeats without update

I tried to do my research on GPS issues before posting here. When I tested my code, it repeats the same output again and again. The gpsStart() method is called by the timer. Permissions for minor and coarse permissions have been added to the manifest. The appendLog() method saves the output in a file.

 public void gpsStart() { // Acquire a reference to the system Location Manager LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); // Define a listener that responds to location updates LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { // Toast.makeText(getApplicationContext(), "location changed", // Toast.LENGTH_SHORT).show(); // Called when a new location is found by the network location // provider. appendLog("Lat: " + location.getLatitude() + "\nLng: " + location.getLongitude()+"\n"); text3.setText("Lat: " + location.getLatitude() + "\nLng: " + location.getLongitude()); } public void onStatusChanged(String provider, int status, Bundle extras) { } public void onProviderEnabled(String provider) { } public void onProviderDisabled(String provider) { } }; // Register the listener with the Location Manager to receive location // updates locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, locationListener); locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); } 
+4
source share
3 answers

Thanks so much for your answers. Dharmendra, you are right, I had to close LocationListeners, however, the answer to my question was much simpler. I needed to change the minimum time (listener timeout to get an answer) to a value above zero. The value I need to change is shown in bold below (MINTIME).

 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MINTIME, 0, gpslocationListener); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MINTIME, 0, networklocationListener); 
0
source

You should not call the gpsStart() method on a timer. I'm going to say how the Listening List works

What is a location receiver?

Location Listener is a class that notifies you when your current location is changed. To receive location updates, you will need to register the LocationListener class using the LocationManager class.

When to register a location receiver?

It depends on the requirements of your application. For example, if you want the location manager to display the current location on the map, you need to register the location receiver in the onCreate() or onResume() method of activity and unregister the recipient in the onPause() or onStop() method. If you want to get the location, even if your application is not running, you can use the service to get the location.

How to register / unregister a location receiver?

To register a LocationListener, you first need an instance of the LocationManager class, which you can get using a context, for example

LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

After that, you will need to install a location provider. There are two types of providers that are often used.

  • GPS Provider
  • Network provider

Now, to register a location receiver with this location provider, there is a requestLocationUpdates LocationManager method. In this method, the first argument is the second argument to the provider β€” this is the minimum time to update the location of the request. The third argument is the minimum distance for a location change request. The biggest argument is for locationListener. Here, how you can use the method

 locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, minTime, minDistance, locationListener); locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, minTime, minDistance, locationListener); 

In order not to update location updates, you can use the method below.

 locationManager.removeUpdates(locationListener) 

Note. . You call the gpsStart method on a timer, as you mentioned in your question, so that it adds a location receiver every time this method calls. So all the listeners launch you a new place, so you probably get the same location several times. Instead, you should call this method once when your activity begins, and unRegister this locationListener when your activity ends.

Hope you get it .: D

Enjoy !!!

+3
source

You better create 2 listeners .1 for Gps and others for the network.

following code examples

 public class MyLocationListener extends LocationListener { public void onLocationChanged(Location location) { // Toast.makeText(getApplicationContext(), "location changed", // Toast.LENGTH_SHORT).show(); // Called when a new location is found by the network location // provider. appendLog("Lat: " + location.getLatitude() + "\nLng: " + location.getLongitude()+"\n"); text3.setText("Lat: " + location.getLatitude() + "\nLng: " + location.getLongitude()); } public void onStatusChanged(String provider, int status, Bundle extras) { } public void onProviderEnabled(String provider) { } public void onProviderDisabled(String provider) { } } MyLocationListener gpsListener=new MyLocationListener(); MyLocationListener networkListener=new MyLocationListener(); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, gpslocationListener); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, networklocationListener); 
+1
source

All Articles