2012-09-17 7 views
5

El escenario de mi aplicación es que deseo rastrear la ubicación de los empleados. Tengo un receptor de transmisión que escucha la transmisión de arranque del dispositivo y registra un administrador de alarma. Cuando el administrador de alarmas marca, registra dos oyentes de ubicación, uno para escuchar gps y otro para red. Quiero que cuando obtenga la primera actualización de ubicación en el método onLocationChange(), guarde la ubicación y anule el registro de esa escucha de ubicación para que cuando el administrador de alarmas vuelva a marcar, no se vuelva a duplicar. Para anular el registro, tengo escuchas de ubicación como objetos estáticos para acceder a ellos en onLocationChange(). Pero he descubierto que NO está eliminando el agente de escucha de ubicación. Aquí está mi ejemplo de código:LocationManager.removeUpdates (listener) no elimina la escucha de la ubicación

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



     } 


    } 

¿Alguien me puede guiar donde estoy equivocado?

Respuesta

1

Ahh ... que tienen un error tipográfico. En realidad, en MyBroadCastREciever clase I registraba gpsListener doble:

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

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

que está oyente no estaba siendo removido y la red.

+0

entonces ¿cuál es la modificación que hizo? Como si eliminas tu oyente de red, no obtendrás actualizaciones de red. – amandroid

+0

sí porque solo quiero obtener una actualización de ubicación única. –

0

Prueba esto ..

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

puede hacerlo de esta manera con una clase privilegiada y eliminar el oyente 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) { 
       } 

     } 
Cuestiones relacionadas