我需要停止位置侦听器更新。我知道,为了阻止一个位置监听器我需要调用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不正确。
现在位于locationManager.requestLocationUpdates中的myLocationListener(部分突出显示红色,并说:“方法myLocationListener()未定义类型GetCoords”。应用程序的名称是GetCoords,因此您知道该作品的来源。我开始认为Java只是超出了我的能力,我大部分都理解你对我的代码的更正,但不明白为什么它仍然不起作用。 –
从'myLocationListener()'中删除该对象,使它简单地成为'myLocationListener'或者,如果我只是按照你的确切指令而不是假设我知道我是什么,那么你可能会想要调用一个像stopMyLocation()这样的方法来访问一个没有括号的变量 – Sam
在做我会一直很好。我有myLocationListener(),这是不正确的,根据你告诉我它是不是ded是myLocationListener,没有括号。非常感谢你的帮助。 –