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) {
}
}
}