LocationManager.removeUpdates (listener) not deleting the location receiver

My application scenario is that I want to track the location of employees. I have a broadcast receiver that listens on a device boot broadcast and registers an alarm manager. When the alarm manager is ticking, it registers two location receivers, one for listening to gps and the other for the network. I want that when I receive the first location update in the onLocationChange () method, save the location and unregister this location receiver so that after the alarm manager loops again, it will not be duplicated. To unregister, I have location receivers as static objects to access them in onLocationChange (). But I found that it does NOT delete the location receiver. Here is my sample code:

 public class BootTimeServiceActivator extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Calendar calendar = Calendar.getInstance();
        AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent mIntent = new Intent(context, MyBroadCastReceiver.class);
        PendingIntent mPendingIntent = PendingIntent.getBroadcast(context, 0, mIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 20 * 60 * 1000, mPendingIntent);


    }

    }

//..........

    public class MyBroadCastReceiver extends BroadcastReceiver{

    public static LocationManager locationManager;
    public static MyLocationListener networkLocationListener;
    public static MyLocationListener gpsLocationListener;



    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Toast.makeText(context, "Alarm has been called...", Toast.LENGTH_SHORT).show();
        initLocationListeners(context);
        registerLocationListeners();

    }

    public void initLocationListeners(Context context) {
        locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
        networkLocationListener = new MyLocationListener(context);
        gpsLocationListener = new MyLocationListener(context);

    }

    public void registerLocationListeners() {
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 0, gpsLocationListener);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 100, 0, gpsLocationListener);

    }

}

\\.....

    public class MyLocationListener implements LocationListener {

    Context context;

    public MyLocationListener(Context context) {
        this.context = context;
    }

    @Override
    public void onLocationChanged(Location location) {
        if(location != null) {
            SDCardService sdService = new SDCardService(context);
            try {
                sdService.logToDB(location);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Log.d("provider",location.getProvider());
            if(location.getProvider().equals(LocationManager.GPS_PROVIDER)) {
                MyBroadCastReceiver.locationManager.removeUpdates(MyBroadCastReceiver.gpsLocationListener);
            }
            else if(location.getProvider().equals(LocationManager.NETWORK_PROVIDER)) {
                MyBroadCastReceiver.locationManager.removeUpdates(MyBroadCastReceiver.networkLocationListener);
            }



        }


    }

- , ?

+5
3

... . MyBroadCastREciever gpsListener :

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
    100, 0, 
    gpsLocationListener);

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 
    100, 0, 
    networkLocationListener); //previously was gpsLocationListener again. Wrong.

y .

+1

.

lmNetwork.removeUpdates(networkLocationListener);
lmGps.removeUpdates(gpsLocationListener);
0

you can do this using the insider class and remove the onPause () listener:

        @Override
        protected void onPause() {
                super.onPause();
                GPSlocationManager.removeUpdates(GPSLocationListener);
                CellLOcationManager.removeUpdates(NetworkLocationListener);
        }

        private void SetLocationManager() {
                GPSlocationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
                CellLOcationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                NetworkLocationListener = new MyNetworkLocationListener();
                GPSLocationListener = new MyGPSLocationListener();
                GPSlocationManager.requestLocationUpdates("gps", 5000, 1, GPSLocationListener);
                CellLOcationManager.requestLocationUpdates("network", 10000, 1, NetworkLocationListener);
        }

        private class MyNetworkLocationListener implements LocationListener {

                @Override
                public void onLocationChanged(Location location) {

                }

                @Override
                public void onStatusChanged(String s, int i, Bundle bundle) {
                }

                @Override
                public void onProviderEnabled(String s) {
                }

                @Override
                public void onProviderDisabled(String s) {
                }

        }
0
source

All Articles