LocationManager.removeUpdates(监听)不删除位置监听器 [英] LocationManager.removeUpdates(listener) not removing location listener
问题描述
我的应用程序的情况是,我想跟踪员工的位置。我有监听设备启动广播和注册一个报警经理一个BroadcastReceiver。当警报管理蜱,它注册了两个位置的听众,人听GPS等网络。我想,当我在onLocationChange得到了第一的位置update()方法,保存位置和取消注册位置监听器,这样当报警经理再次蜱,它不会重复。要注销,我有听众位置作为静态对象来访问他们onLocationChange()。但我发现它不删除位置监听器。这里是我的code例如:
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?
推荐答案
啊...我有一个错字错。实际上,在MyBroadCastREciever类我注册gpsListener两倍:
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.
这篇关于LocationManager.removeUpdates(监听)不删除位置监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!