Issue
I have an android app that connects to the internet to do a reverse geocode on lat and long co-ordinates in real time. This lags the UI and can cause it to give a "not responding - force close/wait" error(as expected without using async/ threads-handlers!)
I have implemented an Async Task which works very well(no more UI lag!), but variables set inside the onLocationChanged method (that sets them to the latitude & longitude) are inaccessible. The class I am doing this in, is linked to another seperate class that passes the variables and can send an sms to a person which includes the reverse-geocoded address. However, since implementing the Async Task using the doinbackground method, I can no longer send what I need. I don't know if I can explain any better, I will provide sample code.
public void onLocationChanged(Location loc){
mylat = loc.getLatitude();
mylong = loc.getLongitude();
}
/*
afaik, this is the code that does the internet stuff, that lags the
UI. If I put this in doinbackground, it cannot access mylat & my long,
and the "loc.getLatitude" has to be done in the "onLocationChanged"
method, which HAS to exist. If I leave the ListAddress code outside the
doinbackground, everything works, BUT the UI will obviously lag.
*/
List<Address> addresses = geoCoder.getFromLocation(mylat,mylong, 1);
Can anyone advise the best way to get mylat = loc.getLatitude(); & mylat = loc.getLatitude();
inside the doinbackground method?
Solution
- Override the constructor of your AsyncTask and set
mylat
andmylong
as variables in that class or pass them to the doInBackground method directly.
... extends AsyncTask<Double, ProgressType, ReturnType> { protected ReturnType doInBackground(Double... params) { mylat = params[0]; mylong = params[1]; List addresses = geoCoder.getFromLocation(mylat, mylong, 1); ... } ... }
and call it with
execute(mylat, mylong);
Answered By - red
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.