我需要停止位置监听器更新。我知道为了阻止位置监听器,我需要拨打locationManager.removeUpdates(LocationListener)
,但我一直收到错误MyLocationListener cannot be resolved to a variable
这是我的代码:
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class GetCoords extends Activity {
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; //in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 10000; //in Milliseconds
protected LocationManager locationManager;
protected Button getLocationButton;
protected Button stopLocationButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_get_coords);
getLocationButton = (Button) findViewById(R.id.get_location_button);
stopLocationButton = (Button) findViewById(R.id.stop_location_button);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationListener()
);
getLocationButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showCurrentLocation();
//String message = "GPS Service Started";
Toast.makeText(GetCoords.this, "GPS Service Started", Toast.LENGTH_SHORT).show();
}
});
stopLocationButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
stopMyLocation();
//locationManager = null;
//stopCurrentLocation();
Toast.makeText(GetCoords.this, "GPS Service Stopped", Toast.LENGTH_SHORT).show();
}
});
}
protected void stopMyLocation() {
locationManager.removeUpdates(MyLocationListener);
locationManager = null;
}
我不明白自己做错了什么,我看到的每个关于如何停止GPS更新的例子让我觉得我的代码在stopMyLocation()下是正确的。我已经在stopLocationButton点击监听器中尝试了它,在它的当前位置,几乎无处不在,它一直告诉我MyLocationListener不正确。
答案 0 :(得分:3)
您需要将位置监听器分配给字段..如
protected MyLocationListener listener;
然后在你的onCreate:
listener = new MyLocationListener();
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
listener);
你的取消注册将会是
locationManager.removeUpdates(listener);
答案 1 :(得分:2)
保存对LocationListener的引用:
protected LocationManager locationManager;
protected MyLocationListener myLocationListener = new MyLocationListener();
改变这个:
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
myLocationListener
);
现在你可以用以下方法取消它:
locationManager.removeUpdates(myLocationListener);
答案 2 :(得分:1)
您已初始化MyLocationListener,但未将其分配给稍后要使用的变量。 Java如何知道您在removeUpdates()
调用中引用的MyLocationListener类的哪个实例?
创建一个类变量LocationListener abc;
在onCreate()
方法中添加行abc = new MyLocationListener();在您注册更新之前。
在requestLocationUpdates()
方法中传递此内容。
在removeUpdates(abc)
方法中致电stopMyLocation()
。
答案 3 :(得分:1)
您收到错误,因为stopMyLocation()函数将未知变量MyLocationListener传递给removeUpdates:
locationManager.removeUpdates(MyLocationListener);
要将现有变量传递给removeUpdates,您已经定义了MyLocationListener类的对象变量,该变量可以在onCreate()和stopMyLocation()函数中访问。
private MyLocationListener locationListener;
locationListener变量可以在onCreate()函数中实例化,并且必须用于调用requestLocationUpdates(...)函数。
locationListener = new MyLocationListener();
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
locationListener
);
答案 4 :(得分:0)
我也有同样的问题。我认为我的问题类似于上面提到的问题,只要创建了不同的侦听器实例。我最终做的是创建一个静态侦听器并创建一个单例方法来防止创建另一个侦听器实例。代码看起来像这样:
static LocationListener mLocationListener = null;
public LocationListener getLocationListener(){
if(mLocationListener==null)
mLocationListener = new MyLocationListener();
return mLocationListener;
}
然后我用这种方式
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000 * 2, 0, getLocationListener());
locationManager.removeLocationUpdates(getLocationListener());
如果有必要,您可以与位置管理员做同样的事情,但我也没有。希望这有帮助!