LocationManager.removeUpdates(listener) not removing location listener

删除回忆录丶 提交于 2020-01-03 12:58:00

问题


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?


回答1:


Try this..

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



回答2:


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.




回答3:


you can do it this way with an insider class and remove the listener onPause() :

        @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) {
                }

        }


来源:https://stackoverflow.com/questions/12458070/locationmanager-removeupdateslistener-not-removing-location-listener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!