如何返回的Andr​​oid LocationListener的数据 [英] How to return data in Android LocationListener

查看:447
本文介绍了如何返回的Andr​​oid LocationListener的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个应用程序中,用户可以使该标签与手机的当前位置照片。所以制作照片后,用户可以通过触摸保存按钮,保存图片。如果按钮被触摸的当前位置会用我自己的LocationListener的类来确定。监听级显示Progressdialog并驳回,一个位置后发现。但现在我想知道,我可以返回的位置回调用活动,因为位置监听器方法是回调方法。是否有一个最佳实践的解决方案,或者有任何线索?

位置监听器:

 公共类MyLocationListener实现LocationListener的{私人ProgressDialog progressDialog;
私人语境mContext;
私人的LocationManager的LocationManager;公共MyLocationListener(上下文的背景下,ProgressDialog对话){
    mContext =背景;
    progressDialog =对话框;
}公共无效startTracking(){
    的LocationManager =(的LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE);
    标准标准=新标准();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
    字符串提供商= locationManager.getBestProvider(标准,真正的);
    locationManager.requestLocationUpdates(提供商,10,10,这一点);
    progressDialog.show();
}私人无效finishTracking(地点){
    如果(位置!= NULL){
        locationManager.removeUpdates(本);
        progressDialog.hide();
        Log.i(跟踪,location.toString());
    }
}@覆盖
公共无效onLocationChanged(地点){
    finishTracking(位置);
}@覆盖
公共无效onProviderDisabled(字符串提供商){}@覆盖
公共无效onProviderEnabled(字符串提供商){}@覆盖
公共无效onStatusChanged(字符串提供商,INT地位,捆绑演员){}}

调用code:

  ProgressDialog对话框=新ProgressDialog(本);
dialog.setTitle(确定位置......);
新MyLocationListener(这一点,对话).startTracking();


解决方案

为什么不从活动通过自己的回调MyLocationListener和finishTracking调用它的方法是什么?

它是一种常用的委托模式。这样的事情:

 类MyLocationListener实现LocationListener的{
    公共接口myListener的{
        无效onLocationReceiver(位置定位);
    }    私人myListener的侦听器;    公共MyLocationListener(上下文的背景下,ProgressDialog对话框,myListener的侦听器){
        mContext =背景;
        progressDialog =对话框;
        this.listener =侦听器;
    }    私人无效finishTracking(地点){
        如果(位置!= NULL){
            locationManager.removeUpdates(本);
            progressDialog.hide();
            Log.i(跟踪,location.toString());
            listener.onLocationReceiver(位置);
        }
    }
}

和调用:

 新MyLocationListener(这一点,对话框,新MyLocationListener.MyListener(){
    公共无效onLocationReceived(地点){
        text.setText(location.toString());
    }
})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();

解决方案

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

这篇关于如何返回的Andr​​oid LocationListener的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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