Android LocationListener 如何返回数据

发布于 2024-12-21 00:35:43 字数 1680 浏览 2 评论 0原文

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

位置监听器:

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

i want to build an app in which users can make foto which is tagged with the current location of the phone. So after making a foto, the user can save the picture by touching on the "save" button. If the button is touched the current location will be determined with my own LocationListener class. The Listener-class shows a Progressdialog and dismiss it, after a location is found. But now i want to know which i can return the location back to the calling Activity, because the Location listener methods are callback methods. Is there a "best-practice" solution for that, or have anybody a clue?

Location Listener:

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

}

Calling code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

命硬 2024-12-28 00:35:43

为什么不将您自己的回调从 Activity 传递到 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();

Why not pass your own callback from activity to MyLocationListener and call its method from finishTracking?

Its a commonly used delegation pattern. Something like that:

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

and call:

new MyLocationListener(this, dialog, new MyLocationListener.MyListener() {
    public void onLocationReceived( Location location ) {
        text.setText(location.toString());
    }
}).startTracking();
蓝眼睛不忧郁 2024-12-28 00:35:43

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

You can do another simple thing - put your class MyLocationListener inside your Activity itself and modify the field in your activity inside your MyLocationListener itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文