1
我有一个实现LocationListener的Activity。如何在活动中正确清理位置侦听器
public class MyActivity extends MapActivity implements LocationListener
在我的活动,我在OnCreate(注册LocationListener的)
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, distance, this);
在的onDestroy方法,我去除登记为我LocationListener的。
@Override
protected void onDestroy() {
Utils.addDebugMsg(this,"onDestroy");
lm.removeUpdates(this);
super.onDestroy();
}
在我的应用程序,我可以改变minTime和距离,让我重新初始化像这样我的听众:
private void initializeGpsListener() {
lm.removeUpdates(this);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, distance, this);
}
出于调试目的,我写的东西在屏幕上每当提供商启用(在这种情况下是GPS)。
@Override
public void onProviderEnabled(String provider) {
Utils.addDebugMsg(this,"onProviderEnabled : " + provider);
}
我注意到的是,有时候,我的活动(或位置侦听器)的多个实例“保持在”周围。每次打开GPS提供程序时,我都会看到几条不同的活动打印此行(同时显示),而不是看到1条语句“onProviderEnabled:GPS”。
如何清理这些侦听器(=我的活动),并确保在整个应用程序中只保留1个活动。
只是想我会提到我解决了这个问题。 – ddewaele 2010-11-08 22:59:33
Activity实现了OnSharedPreferenceChangeListener。在onCreate期间,该活动已注册为PreferenceChangelistener,但未在onDestroy()中注销。因此,即使在活动被破坏之后,仍然存在对该活动的引用,从而导致重复的消息。 – ddewaele 2010-11-08 23:02:32