2012-09-17 18 views
5

Uygulama senaryolarım, çalışanların konumunu izlemek istiyorum. Cihaz önyükleme yayınını dinleyen ve bir alarm yöneticisi kaydeden bir yayın alıcım var. Alarm yöneticisi işaretlendiğinde, biri gps'yi ve ağ için diğerlerini dinlemek üzere iki konum dinleyiciyi kaydeder. OnLocationChange() yönteminde ilk konum güncellemesini aldığımda, konumu kaydedip bu konum dinleyicisinin kaydını sildiğimde bunu, alarm yöneticisi tekrar işaretlediğinde yinelenmez. Kayıtları kaldırmak için, konum dinleyicilerine onLocationChange() öğesinde erişecek statik nesneler olarak sahibim. Ancak konum dinleyicisini kaldırmanın YANLIŞ olduğunu anlamıştım. İşte benim kod örneğim:LocationManager.removeUpdates (listener) konum dinleyicisini kaldırmıyor

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



     } 


    } 

Yanlış nerede olduğumu bana rehberlik edebilir misiniz?

cevap

1

Ahh ... Ben bir yazım hatası hata var. y ağ dinleyicisi kaldırılmakta değildi oluyor

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

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

: Aslında MyBroadCastREciever sınıfta ben iki kat gpsListener kayıt edildi.

+0

o zaman yaptığımız değişiklik nedir? Ağ dinleyicinizi kaldırırsanız ağ güncellemelerini alamayacaksınız. – amandroid

+0

evet çünkü tek bir yer güncellemesi almak istiyorum. –

0

bu deneyin ..

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

bunu içeriden sınıfı ile bu şekilde yapmak ve dinleyici OnPause() kaldırabilirsiniz:

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

     } 
İlgili konular