【问题标题】:How to return data in Android LocationListener如何在 Android LocationListener 中返回数据
【发布时间】:2011-12-14 17:19:55
【问题描述】:

我想构建一个应用程序,用户可以在其中制作带有手机当前位置标记的照片。所以在拍完照片后,用户可以通过点击“保存”按钮来保存照片。如果按钮被触摸,当前位置将由我自己的 LocationListener 类确定。侦听器类显示一个进度对话框并在找到位置后将其关闭。但现在我想知道哪个可以将位置返回给调用活动,因为位置侦听器方法是回调方法。有没有“最佳实践”的解决方案,或者有人知道吗?

位置监听器:

public class MyLocationListener implements LocationListener {

private ProgressDialog progressDialog;
private Context mContext;
private LocationManager locationManager;

public MyLocationListener(Context context, ProgressDialog dialog) {
    mContext = context;
    progressDialog = dialog;
}

public void startTracking() {
    locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
    String provider = locationManager.getBestProvider(criteria, true);
    locationManager.requestLocationUpdates(provider, 10, 10, this);
    progressDialog.show();
}

private void finishTracking(Location location) {
    if(location != null) {
        locationManager.removeUpdates(this);
        progressDialog.hide();
        Log.i("TRACKING",location.toString());
    }
}

@Override
public void onLocationChanged(Location location) {
    finishTracking(location);
}

@Override
public void onProviderDisabled(String provider) { }

@Override
public void onProviderEnabled(String provider) { }

@Override
public void onStatusChanged(String provider, int status, Bundle extras) { }

}

调用代码:

ProgressDialog dialog = new ProgressDialog(this);
dialog.setTitle("Determine position...");
new MyLocationListener(this, dialog).startTracking();

【问题讨论】:

    标签: android localization android-location locationlistener


    【解决方案1】:

    为什么不将您自己的回调从活动传递到 MyLocationListener 并从 finishTracking 调用它的方法?

    它是一种常用的委托模式。类似的东西:

    class MyLocationListener implements LocationListener {
        public interface MyListener {
            void onLocationReceiver( Location location );
        }
    
        private MyListener listener;
    
        public MyLocationListener(Context context, ProgressDialog dialog, MyListener listener) {
            mContext = context;
            progressDialog = dialog;
            this.listener = listener;
        }
    
        private void finishTracking(Location location) {
            if(location != null) {
                locationManager.removeUpdates(this);
                progressDialog.hide();
                Log.i("TRACKING",location.toString());
                listener.onLocationReceiver(location);
            }
        }
    }
    

    然后调用:

    new MyLocationListener(this, dialog, new MyLocationListener.MyListener() {
        public void onLocationReceived( Location location ) {
            text.setText(location.toString());
        }
    }).startTracking();
    

    【讨论】:

      【解决方案2】:

      您可以做另一件简单的事情 - 将您的课程 MyLocationListener 放入您的 Activity 本身并修改您的活动中的字段在您的 MyLocationListener 本身内。

      【讨论】:

      • 在我的第一个版本中,我有这个解决方案,但我想要一个抽象两个类的解决方案。还是谢谢。
      猜你喜欢
      • 2015-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多