Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LocationManager.removeUpdates(listener) not removing location listener

My app scenario is that I want to track employees location. I have a broadcastreceiver which listens device boot broadcast and register an alarm manager. When alarm manager ticks, it registers two location listeners, one to listen to gps and other for network. I want that when I got first location update in onLocationChange() method, save location and unregister that location listener so that when alarm manager ticks again, it does not get duplicate. To unregister, I have location listeners as static objects to access them in onLocationChange(). But I have found that it is NOT removing location listener. Here is my code example:

 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);
            }



        }


    }

Can anyone guide me where I am wrong?

like image 785
Khawar Raza Avatar asked Nov 03 '22 15:11

Khawar Raza


2 Answers

Try this..

lmNetwork.removeUpdates(networkLocationListener);
lmGps.removeUpdates(gpsLocationListener);
like image 132
raghav chopra Avatar answered Nov 15 '22 00:11

raghav chopra


Ahh... I have a typo mistake. Actually in MyBroadCastREciever class I was registering gpsListener twice as:

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

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

that's y network listener was not being removed.

like image 44
Khawar Raza Avatar answered Nov 15 '22 00:11

Khawar Raza