我想停止LocationListener
,但使用 a Log
,我发现我的服务仍在运行 afterremoveUpdates()
方法(当位置更改时,它会插入到我的日志中)。你知道问题出在哪里吗?提前致谢。
public class MyService extends Service implements LocationListener {
private final static String TAG = "MyService";
LocationManager lm;
public MyService() {
}
....//Other methods here
public void onLocationChanged(Location loc) {
Log.d(TAG, loc.toString());
if(/*A condition, which has to stop the service when it´s time*/)
{
lm.removeUpdates(this);
lm = null;
}
}
public void subscribeToLocationUpdates() {
this.lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
this.lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
@Override
public void onDestroy() {
}
}
我在另一个 Activity 中调用此服务,onCreate 方法:
ComponentName comp = new ComponentName(getPackageName(),
MyService.class.getName());
ComponentName service = startService(new Intent().setComponent(comp));