2012-09-17 306 views
5

我的應用場景是我想跟蹤員工的位置。我有一個廣播接收器,它偵聽設備引導廣播並註冊一個警報管理器。當警報管理器滴答時,它會註冊兩個位置監聽器,一個監聽gps和其他網絡。我希望當我在onLocationChange()方法中獲得第一個位置更新時,保存位置並取消註冊該位置偵聽器,以便當警報管理器再次打勾時,它不會重複。要取消註冊,我將位置偵聽器作爲靜態對象在onLocationChange()中訪問它們。但我發現它不是刪除位置偵聽器。這裏是我的代碼示例:LocationManager.removeUpdates(偵聽器)不刪除位置偵聽器

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



     } 


    } 

任何人都可以引導我,我錯了嗎?

回答

1

啊...我有一個錯字錯。實際上,在MyBroadCastREciever類我註冊gpsListener兩倍:

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

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

說的Y網絡監聽器沒有被刪除。

+0

那麼什麼是你做的修改?就好像你刪除了你的網絡監聽器你不會得到網絡更新。 – amandroid

+0

是的,因爲我只想獲得單個位置更新。 –

0

試試這個..

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

你可以用一個內部類這樣做,並刪除偵聽的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) { 
       } 

     }