0

我需要停止位置监听器更新。我知道为了停止我需要调用的位置侦听器,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 不正确。

4

5 回答 5

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);
于 2012-11-15T17:36:47.347 回答
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);
于 2012-11-15T17:36:44.573 回答
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
    );
于 2012-11-15T17:47:23.110 回答
1

您已初始化 MyLocationListener,但未将其分配给稍后使用的变量。Java 怎么知道你在removeUpdates()调用中指的是哪个 MyLocationListener 类的实例?

  1. 创建一个类变量LocationListener abc;

  2. onCreate()方法中添加行 abc = new MyLocationListener(); 在您注册更新之前。

  3. requestLocationUpdates()在你的方法中传递这个。

  4. 调用removeUpdates(abc)你的stopMyLocation()方法。

于 2012-11-15T17:38:55.393 回答
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());

如有必要,您可以对位置管理器执行相同的操作,但我也没有。希望这可以帮助!

于 2014-10-12T20:14:30.503 回答