0

I created a class GPSController. The onReceive is called every Minute.

I want to get the current GPS Data with the GPSListener in my AsyncTask. But the onLocationChanged Method is never called. :-/

Here is my Class:

public class GPSController extends SensorTimeController {

    // The minimum distance to change Updates in meters
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0;
    // The minimum time between updates in milliseconds
    private static final long MIN_TIME_BW_UPDATES = 2;
    private boolean isGPSEnabled = false;
    private boolean isNetworkEnabled = false;
    private double latitude = -1;
    private double longitude = -1;
    private double altitude = -1;
    private double speed = 0L;
    private double bearing = 0L;
    private LocationManager locationManager;
    private GPSLocationTask backgroundTask;

    public GPSController(GPSModule module) {
        super(module);
        backgroundTask = new GPSLocationTask();
    }

    private void SensorDataFinished(double longitude, double latitude, double altitude, double speed, double bearing) {
        Date date = new Date(System.currentTimeMillis());
        SensorRecord record = new SensorRecord(module.getNextIndex(), date, structure);
        if (longitude != -1)
            record.addData("longitude", String.valueOf(longitude));
        else
            record.addData("longitude", " ");

        if (latitude != -1)
            record.addData("latitude", String.valueOf(longitude));
        else
            record.addData("latitude", " ");

        if (altitude != -1)
            record.addData("altitude", String.valueOf(altitude));
        else
            record.addData("altitude", " ");

        if (bearing != 0L)
            record.addData("bearing", String.valueOf(bearing));
        else
            record.addData("bearing", " ");

        if (speed != 0L)
            record.addData("speed", String.valueOf(speed));
        else
            record.addData("speed", " ");

        module.log(record);

    }


    @Override
    public void onReceive(Context context, Intent intent) {
        locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        backgroundTask.execute();
    }

    private class GPSLocationTask extends AsyncTask<Void, Void, Void> implements LocationListener {

        @Override
        protected Void doInBackground(Void... params) {
            System.out.println("THREAD STARTED");
            try {
                Looper.prepare();
                Looper.loop();
                if(isNetworkEnabled)
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                if(isGPSEnabled)
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
            }catch(SecurityException e) {
                e.printStackTrace();
            }
            return null;
        }


        public void onLocationChanged(Location location) {
            if (location != null) {
                longitude = location.getLongitude();
                latitude = location.getLatitude();
                altitude = location.getAltitude();

                // don't do anything if we get a null reading for some reason
                if (longitude == 0.0f && latitude == 0.0f)
                    return;

                // any speed information?
                if (location.hasSpeed())
                    speed = (double) location.getSpeed();

                // any bearing information?
                if (location.hasBearing())
                    bearing = (double) location.getBearing();
                System.out.println(longitude+", "+ latitude+", "+ altitude+", "+ speed+", "+ bearing);
                SensorDataFinished(longitude, latitude, altitude, speed, bearing);
            }
        }

        public void onProviderDisabled(String provider) {

        }

        public void onProviderEnabled(String provider) {

        }

        public void onStatusChanged(String provider, int status, Bundle extras) {

        }
    }
}
DarkWing89
  • 319
  • 2
  • 4
  • 10
  • If I am not mistaken, you never call ```Looper.loop()``` – Anis Mar 08 '17 at 11:20
  • Thanks for your answer! I added Looper.loop() after Looper.prepare() but nothing changed :-/ – DarkWing89 Mar 08 '17 at 12:08
  • It may come as a stupid question, but have you moved during the minute? If the GPS doesn't detect any change in the position (upto a certain threshold which should be in the order of tens of centimeters), the event may not be fired, even with a minimal distance equal to zero. Beside, could you update your code to include the modifications you have done? – Anis Mar 08 '17 at 12:40
  • yes, i have already tried to move the phone. My goal is it to see everytime the onReceive is called the GPS Location of the Phone. Maybe i should expand it with a lastknownlocation when everything else works. And is there no location change at the first call from nothing to some location? :-/ – DarkWing89 Mar 08 '17 at 12:49
  • code updated :-) – DarkWing89 Mar 08 '17 at 12:54
  • Have you taken a look at http://stackoverflow.com/questions/36808622/cant-get-current-location-android-on-location-changed . It seems to me that your usage of AsyncTask + loopers is wrong, without mentionning the fact that Looper.quit is never called. See here http://stackoverflow.com/questions/13398626/android-locationmanager-constructors-looper if you really want to use loopers. – Anis Mar 08 '17 at 13:12

0 Answers0