Android的onLocationChanged和MainActivity类别 [英] Android onLocationChanged and MainActivity class

查看:207
本文介绍了Android的onLocationChanged和MainActivity类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

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) {

}

在我MainActivity

and in my MainActivity

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

        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

现在,我要的是一次接收到在MainActivity类设备的当前位置(海拔获得和经度变量在应用以后使用)。

Now, all I want is to receive the current position of the device ONCE to the MainActivity class (get altitude and longitude variables to use later in the application).

一个。我该如何停止单个时间后收到的位置?该功能lm.removeUpdates(听众)只能被称为在MainActivity类。

A. how do I stop receiving the location after a single time? The function lm.removeUpdates(listener) can only be called in the MainActivity class.

乙。基本上是相同的。如何将MyLocationListener类,并在MainActivity之一之间进行连接?

B. basically the same. How do I connect between the MyLocationListener class and the MainActivity one?

对不起,我是一个新手,Android和Java开发。
和感谢!

Sorry, I'm a newbie to Android and Java development. And thanks!

推荐答案

您可以使用下面的示例code:

You may use the following sample code:

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();

这篇关于Android的onLocationChanged和MainActivity类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆