Search code examples
androidgpslocationmanager

What do I see the GPS icon consistently?


I've written my custom location manager to check for an update in the user's location every 30 seconds. The code is working fine i.e. I'm receiving updates in user's location. But the problem is that the GPS icon is always visible on the status bar on top. I'm guessing that it should be visible only once in 30 seconds. Is this normal or I'm doing something wrong?

enter image description here

public volatile Double mLatitude = 0.0;
public volatile Double mLongitude = 0.0;

int minTime = 30000;
float minDistance = 0;
MyLocationListener myLocListener = new MyLocationListener();
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

Criteria criteria = new Criteria();
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setSpeedRequired(false);

String bestProvider = locationManager.getBestProvider(criteria, false);
locationManager.requestSingleUpdate(criteria, myLocListener, null);     
locationManager.requestLocationUpdates(bestProvider, minTime, minDistance, myLocListener);


private class MyLocationListener implements LocationListener {
        @Override
        public void onLocationChanged(Location loc)
        {
            if (loc != null) {
                //Do something knowing the location changed by the distance you requested
                mLatitude = loc.getLatitude();
                mLongitude = loc.getLongitude();
                Toast.makeText(mContext, "Location Changed! "+Double.toString(mLatitude)+" "+Double.toString(mLongitude), Toast.LENGTH_LONG).show();
            }

        }

        @Override
        public void onProviderDisabled(String arg0)
        {
            //Do something here if you would like to know when the provider is disabled by the user
            Toast.makeText(mContext, "Provider Disabled! "+Double.toString(mLatitude)+" "+Double.toString(mLongitude), Toast.LENGTH_LONG).show();

        }

        @Override
        public void onProviderEnabled(String arg0)
        {
            //Do something here if you would like to know when the provider is enabled by the user
            Toast.makeText(mContext, "Provider Enabled! "+Double.toString(mLatitude)+" "+Double.toString(mLongitude), Toast.LENGTH_LONG).show();

        }

        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2)
        {
            //Do something here if you would like to know when the provider status changes
            Toast.makeText(mContext, "Provider Status Changed! "+Double.toString(mLatitude)+" "+Double.toString(mLongitude), Toast.LENGTH_LONG).show();

        }
    }

Solution

  • The question is old but still, it would be great to give a better answer.

    You need to use a Handler which runs continuously on 5 minutes interval so, you get the location update only once and you release the GPS device, this way your app won't listen to the GPS updates but your handler know when it should be called again to listen the updates.

    public static void getLocation(Context context) {
    
        Handler locationHandler = new Handler();
    
        locationHandler.post(new Runnable() {
            @Override
            public void run() {
                LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
                locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, new LocationListener() {
                    @Override
                    public void onLocationChanged(Location location) {
                        // Handle the location update
                    }
    
                    @Override
                    public void onStatusChanged(String provider, int status, Bundle extras) {
    
                    }
    
                    @Override
                    public void onProviderEnabled(String provider) {
    
                    }
    
                    @Override
                    public void onProviderDisabled(String provider) {
    
                    }
                }, null);
    
                locationHandler.postDelayed(this, 1000 * 60 * 5);
            }
        });
    }