Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

requestLocationUpdates does not update on interval in Android

I have the problem that my service which has the LocationListener implemented, receives a GPSupdate every 500ms it seems. No matter what minTime i put in the requestLocationUpdates function.

Piece of my code:

public class LocationService extends Service implements LocationListener {

    LocationManager locMan;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        locMan = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
        locMan.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER;
        locMan.requestLocationUpdates( LocationManager.GPS_PROVIDER, 60000, 1, this); 

    }

    public void onLocationChanged(Location location) {
        Log.d( "Loc", "Location has been changed" );
    }
}

From a button in my main activity i will call startService() , after this it should run in the background but it gives continuous updates.

like image 263
Bander Avatar asked Nov 05 '22 07:11

Bander


1 Answers

I see a few things wrong with your situation:

The accuracy of GPS is at best about 3 meters, and that's in optimal conditions. So it doesn't make sense to request an update if the position changes by 1 meter. The GPS noise could easily be indicating movement when in fact the device is sitting still. Make the minDistance at least 10 would be my recommendation.

I don't see why you're calling isProviderEnabled() since you don't do anything with the returned value.

You say that after startService() things should be running in the background, but that's not the case. A Service doesn't run in the background unless you create some sort of background thread for it. There's too much to cover in this answer to explain how services work. Go online and do some reading, or get a good book on Android development.

like image 124
Dave MacLean Avatar answered Nov 09 '22 16:11

Dave MacLean