Razvan Cirstei
Razvan Cirstei

Reputation: 43

Android Location: simple one-time GPS fetch using AsyncTask

I try to update the static variables latitude and longitude with last known position.

Class:

    class FetchGPS extends AsyncTask<String, Integer, String> {

    @Override
    protected void onPreExecute() {
        new_latitude = 0.0;
        new_longitude = 0.0;
    }

    @Override
    protected String doInBackground(String... params) {
        LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
        while (new_latitude == 0.0) {
            try {
                Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                new_latitude = location.getLatitude();
                new_longitude = location.getLongitude();
            } catch (Exception e1) {
                try {
                    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    new_latitude = location.getLatitude();
                    new_longitude = location.getLongitude();
                } catch (Exception e2) {
                }
            }
        }
        return null;
    }

In onCreateView:

      try {
        FetchGPS fetchCordinates = new FetchGPS();
        fetchCordinates.execute();
    } catch (Exception e){}

Problem: after 20 seconds with GPS and MobData activated I get 0.0 and 0.0

Upvotes: 1

Views: 455

Answers (1)

kris larson
kris larson

Reputation: 30985

  • using statics in AsyncTask subclasses is not best practice
  • use onPostExecute() to tell you when the background task is finished
  • calling getActivity() from doInBackground() is not best practice

Try this:

public class FetchGPS extends AsyncTask<Void, Void, Double[]> {

    private static final String TAG = "FetchGPS";

    private LocationManager mLocationManager;

    public FetchGPS(Context context) {
        mLocationManager = (LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE);
    }

    @Override
    protected Double[] doInBackground(Void... params) {

        Double[] coords = null;

        try {
            Location location = mLocationManager
                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            coords = new Double[2];
            coords[0] = location.getLatitude();
            coords[1] = location.getLongitude();
        } catch (Exception e) {
            Log.e(TAG, "could not get coordinates", e);
        }

        return coords;
    }
}

In onCreateView():

FetchGPS fetchCordinates = new FetchGPS(this) {

    @Override
    protected void onPostExecute(Double[] result) {

        if (result != null) {
            double latitude = result[0];
            double longitude = result[1];

            // have coordinates, continue on UI thread
        } else {
            // error occurred
        }
    }

};

fetchCordinates.execute();

Note: Overriding onPostExecute() where I did, within onCreateView() isn't really good practice either, I only did this for the example.

Upvotes: 1

Related Questions