Android LocationManager.RemoveUpdate(侦听器)不删除位置侦听器
我的应用程序场景是我想跟踪员工的位置。我有一个broadcastreceiver,它监听设备启动广播并注册报警管理器。当alarm manager发出滴答声时,它会注册两个位置侦听器,一个用于侦听gps,另一个用于网络。我希望当我在onLocationChange()方法中获得第一次位置更新时,保存位置并注销该位置侦听器,以便当alarm manager再次勾选时,它不会重复。要取消注册,我将位置侦听器作为静态对象,以便在onLocationChange()中访问它们。但我发现它并没有删除位置侦听器。下面是我的代码示例:Android LocationManager.RemoveUpdate(侦听器)不删除位置侦听器,android,locationlistener,Android,Locationlistener,我的应用程序场景是我想跟踪员工的位置。我有一个broadcastreceiver,它监听设备启动广播并注册报警管理器。当alarm manager发出滴答声时,它会注册两个位置侦听器,一个用于侦听gps,另一个用于网络。我希望当我在onLocationChange()方法中获得第一次位置更新时,保存位置并注销该位置侦听器,以便当alarm manager再次勾选时,它不会重复。要取消注册,我将位置侦听器作为静态对象,以便在onLocationChange()中访问它们。但我发现它并没有删除位置侦
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);
}
}
}
谁能告诉我哪里错了吗?试试这个
lmNetwork.removeUpdates(networkLocationListener);
lmGps.removeUpdates(gpsLocationListener);
啊。。。我有个打字错误。实际上,在MyBroadcastReceiver类中,我注册了两次gpsListener:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
100, 0,
gpsLocationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
100, 0,
networkLocationListener); //previously was gpsLocationListener again. Wrong.
没有删除y网络侦听器。您可以通过内部类这样做,并在暂停()时删除侦听器:
那你做了什么修改?好像你删除了你的网络侦听器,你就不会得到网络更新。
@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) {
}
}