2

目前我有如下编码用于我在android上的活动。现在我将其用作对象并开始扫描位置并在 3 秒后使用处理程序使用 returnBestLocation 方法取回它的位置。

但是我想问一下,MyLocationListener 对象是否有可能在位置更改时自动返回调用活动,而不是在 3 秒后调用对象返回位置?

public class MyLocationListener implements LocationListener {

    LocationManager locationManager;
    Date currentBestLocationDate;
    Intent notificationIntent;
     Context mContext;
    Location currentBestLocation = null, lastKnownLocation=null;

 public MyLocationListener(Context mContext)
 {this.mContext = mContext;

 }


 public void startLocationScan()
 {
     Log.d(Config.log_id, "Custom Location Listener started");

     if (locationManager == null) {

        locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
        Location locationNETWORK = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (locationNETWORK != null) {
            lastKnownLocation=locationNETWORK;
        }
        Location locationGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (locationGPS != null) {
            lastKnownLocation=locationGPS;

        }
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,  MyLocationListener.this);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,MyLocationListener.this);





    }


 }
 public void stopLocationScan()
 {

        if(locationManager!=null)
        {
            locationManager.removeUpdates(MyLocationListener.this);

             Log.d(Config.log_id, "Custom Location Listener Stopped");
        }
        }
public Location returnBestLocation()
{
    return currentBestLocation;
}

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }


    @Override
    public void onLocationChanged(Location location) {


        // TODO Auto-generated method stub
        if (currentBestLocation == null) {
            currentBestLocation = location;
        }

        long timeDelta = location.getTime() - currentBestLocation.getTime();
        Log.d(Config.log_id, "locationpostingservice's changed with accuracy " + location.getAccuracy() + " s different " + (float) timeDelta / 1000);

        if (timeDelta >= 120000) {
            currentBestLocation = location;
            Log.d(Config.log_id,"posting service Location changed due to over 2min "+ location.getAccuracy() + " s different "+ (float) timeDelta / 1000);

        }


        if (currentBestLocation.getAccuracy() >= location.getAccuracy()) {
            currentBestLocation = location;
        }

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }
4

2 回答 2

4

您可以将要通知的活动作为侦听器传递给 MyLocationListener。创建自己的Listener接口,让Activity实现它并向MyLocationListener添加一个方法,比如addListener()。每次您想要通知这些活动时,都会遍历侦听器列表并调用它们的 locationChanged 方法(或您在接口定义中调用的任何方法)。只要确保为空侦听器活动添加错误处理等。

所以基本上你有自己的自定义监听器来监听 LocationListener。

另一种方法是使用广播接收器并广播位置变化。

于 2012-07-10T13:32:45.497 回答
0

正如 Fraggle 所说,正确的方法是创建您自己的监听器接口,在您的类中实现它,然后从您的活动中调用您的监听器。

但是一个快速n-dirty的替代方法可能是在你的Activity中实现LocationListener接口,而不是把它转变成一个新的类(MyLocationListener)。然后只要把你要运行的所有代码都放到onLocationChanged中,不用担心与其他对象通信。

于 2012-07-11T11:25:17.253 回答