Java Android onLocationChanged和MainActivity类

Java Android onLocationChanged和MainActivity类,java,android,class,gps,locationlistener,Java,Android,Class,Gps,Locationlistener,我有以下代码: public class MyLocationListener implements LocationListener { @Override public void onLocationChanged(Location loc) { // called when the listener is notified with a location update from the GPS Log.d("Latitude", Double.toString(loc.

我有以下代码:

public class MyLocationListener implements LocationListener {


@Override
public void onLocationChanged(Location loc) {
    // called when the listener is notified with a location update from the GPS
    Log.d("Latitude", Double.toString(loc.getLatitude()));
    Log.d("Longitude", Double.toString(loc.getLongitude()));
}
@Override
public void onProviderDisabled(String provider) {
   // called when the GPS provider is turned off (user turning off the GPS on the phone)
}
@Override
public void onProviderEnabled(String provider) {
   // called when the GPS provider is turned on (user turning on the GPS on the phone)
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}
在我的主要活动中

            LocationListener locationListener = new MyLocationListener();
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
现在,我只想将设备的当前位置发送到MainActivity类(获取海拔高度和经度变量,以便稍后在应用程序中使用)

A.如何在一次之后停止接收位置?只能在MainActivity类中调用函数lm.removeUpdates(listener)

基本相同。如何连接MyLocationListener类和MainActivity类

对不起,我是Android和Java开发的新手。
谢谢

您可以使用以下示例代码:

public class LocationGetter {
    private final Context context;
    private Location location = null;
    private final Cordinate gotLocationLock = new Cordinate();
    private final LocationResult locationResult = new LocationResult() {
        @Override
        public void gotLocation(Location location) {
            synchronized (gotLocationLock) {
                LocationGetter.this.location = location;
                gotLocationLock.notifyAll();
                Looper.myLooper().quit();
            }
        }
    };

    public LocationGetter(Context context) {
        if (context == null)
            throw new IllegalArgumentException("context == null");

        this.context = context;
    }

    public  void getLocation(int maxWaitingTime, int updateTimeout) {
        try {
            final int updateTimeoutPar = updateTimeout;
            synchronized (gotLocationLock) {
                new Thread() {
                    public void run() {
                        Looper.prepare();
                        LocationResolver locationResolver = new LocationResolver();
                        locationResolver.prepare();
                        locationResolver.getLocation(context, locationResult, updateTimeoutPar);
                        Looper.loop();
                    }
                }.start();

                gotLocationLock.wait(maxWaitingTime);
            }
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
        gteAddress ();
    }


public double getLatitude() {
    return location.getLatitude();
}

public double getLongitude() {
    return location.getLongitude();
}
在您的活动中使用:

_locationGetter=new LocationGetter(context);

_locationGetter.getLocation(200000000, 10000000);

_locationGetter.getLongitude();

_locationGetter.getLatitude();

您可以使用以下示例代码:

public class LocationGetter {
    private final Context context;
    private Location location = null;
    private final Cordinate gotLocationLock = new Cordinate();
    private final LocationResult locationResult = new LocationResult() {
        @Override
        public void gotLocation(Location location) {
            synchronized (gotLocationLock) {
                LocationGetter.this.location = location;
                gotLocationLock.notifyAll();
                Looper.myLooper().quit();
            }
        }
    };

    public LocationGetter(Context context) {
        if (context == null)
            throw new IllegalArgumentException("context == null");

        this.context = context;
    }

    public  void getLocation(int maxWaitingTime, int updateTimeout) {
        try {
            final int updateTimeoutPar = updateTimeout;
            synchronized (gotLocationLock) {
                new Thread() {
                    public void run() {
                        Looper.prepare();
                        LocationResolver locationResolver = new LocationResolver();
                        locationResolver.prepare();
                        locationResolver.getLocation(context, locationResult, updateTimeoutPar);
                        Looper.loop();
                    }
                }.start();

                gotLocationLock.wait(maxWaitingTime);
            }
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
        gteAddress ();
    }


public double getLatitude() {
    return location.getLatitude();
}

public double getLongitude() {
    return location.getLongitude();
}
在您的活动中使用:

_locationGetter=new LocationGetter(context);

_locationGetter.getLocation(200000000, 10000000);

_locationGetter.getLongitude();

_locationGetter.getLatitude();

您还可以在获得坐标(并可能检查坐标是否足以满足您的需要)后使用LocationManager.RemoveUpdate:


您还可以在获得坐标(并可能检查坐标是否足以满足您的需要)后使用LocationManager.RemoveUpdate:


谢谢,我没想过!但是我如何在课堂上使用它呢?我怎么知道它什么时候得到一个值呢?在活动中使用线程来获取位置&当线程完成时调用getLongitude()&&getLatitude()方法,它会通知您。谢谢,我没有想到这一点!但是我如何在课堂上使用它呢?我如何知道它何时获取值?在活动中使用线程获取位置&线程完成时调用getLongitude()&&getLatitude()方法通知您。只需将MyLocationManager设置为MainActivity的内部类。只需将MyLocationManager设置为MainActivity的内部类。