Search code examples
androidlocalizationandroid-locationlocationlistener

How to return data in Android LocationListener


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

Solution

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