2012-09-16 31 views
1

我有以下代码:的Android onLocationChanged和MainActivity

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

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

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

现在,我要的是一次接收设备的当前位置在MainActivity类(获取高度和经度变量以便在应用程序中稍后使用)。

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

B.基本一致。如何在MyLocationListener类和MainActivity之间进行连接?

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

回答

1

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

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

谢谢,我没有想到这个! 但是我怎么能和我的班级一起使用它?我怎样才能知道它何时获得价值? – user1591982

+0

在您的活动中使用线程获取位置以及线程完成时调用getLongitude()&& getLatitude()方法通知您。 – yokees

0

您还可以使用LocationManager.removeUpdates obtining坐标(也可能是检查后如果坐标足以满足您的需求):

 @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())); 
     lm.removeUpdates(this); 
    } 
+0

但我无法引用lm,因为它在MainActivity类中。 – user1591982

+0

只是使MyLocationManager成为MainActivity的内部类。 – toommm