How to return data in Android LocationListener

戏子无情 提交于 2019-12-12 04:58:46

问题


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

回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/8508828/how-to-return-data-in-android-locationlistener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!