Issue
I'm working on an app that provides you with the average speed between two points, but I can't find a way to separate the "location logic" in a separate AsyncTask. I have to check if you are in one of the two(starting/finishing) points and then add to a list the instant speed and calculate every time the average and display it. I'd like to use the LocationListener, but how I can use it inside an Async task?
In the AsyncTask (i've already all the permission, asked in the main activity):
protected String doInBackground(Integer... integers) {
Looper.prepare();
locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
Log.d(TAG,"READY");
Log.d(TAG,locationListener.);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, gpsInterval, 0, locationListener);
Log.d(TAG,"DONE");
return "";
}
i see in the logcat the "ready" and the "done" but nothing from myLocationListener
public class MyLocationListener implements LocationListener {
private static final String TAG = "MyLocationListener ";
private MySpeedList speedList= new MySpeedList();
@Override
public void onLocationChanged(Location location) {
Log.d(TAG,Float.toString(location.getSpeed()));
speedList.add(location.getSpeed());
Log.d(TAG,Float.toString(speedList.getAverageSpeed()));
}
...
Does anyone have suggestions? I'm a student, so I'm a beginner in android, this is my first "big project"
Solution
You should execute the async task inside the location listener itself and move these lines out to main thread:
locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
Log.d(TAG,"READY");
Log.d(TAG,locationListener.);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, gpsInterval, 0, locationListener);
Log.d(TAG,"DONE");
return "";
And in your listener:
public class MyLocationListener implements LocationListener {
private static final String TAG = "MyLocationListener ";
private MySpeedList speedList= new MySpeedList();
@Override
public void onLocationChanged(Location location) {
if (calculationsTask == null || calculationsTask.getStatus() == AsyncTask.Status.FINISHED) {
calculationsTask = new CalculationsTask()
calculationsTask.execute(location);
} else {
// buffer pending calculations here or cancel the currently running async task
}
}
...
CalculationsTask
private class CalculationsTask extends AsyncTask<Location, Integer, Long> {
protected Long doInBackground(Location location) {
// do calculations here
return speed;
}
protected void onPostExecute(Long result) {
// this method is executed in UI thread
// display result to user
}
}
Here, you can deliver your calculations result through onPostExecute(...)
method as this method is ran on the main thread. Also note that you cannot execute an async task the second time, so you have to create a new instance every time.
Also, if you want to access speedList
in your async task, you can make the CalculationsTask
an inner class of your MyLocationListener
or just pass it as a parameter.
Answered By - Christilyn Arjona
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.