Scenario:
So what I have done so far is Created an AsyncTask to handle my GeoCoder that updates every 3 min (for testing purposes). Then I set up a TimerTask that shows a toast message with the users current Address every 4 minutes. (TimerTasks not included in code)
So heres the problem:
When I am in my app, everything is fine, however when my app is running in the background, the Toast messages stay stuck at whatever address the app was last set at before I exited my app. I know for sure that the AsyncTask does run in the background (Checked LogCats) and it seems like everything is running in the background fine, I just cant display the current address on my Toast.
All thoughts and inputs will be appreciated!
Here is my code:
public class statuspage extends MapActivity {
LocationManager locationManager;
MapView mapView;
Criteria criteria;
Location location;
Geocoder gc;
Address address;
String bestProvider;
String LOCATION_SERVICE = "location";
String addressString = "Searching for Nearest Address";
StringBuilder sb;
private MapController mapController;
private MyLocationOverlay myLocation;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.statuspage);
// Get Mapping Controllers etc //
mapView = (MapView) findViewById(R.id.mapView);
mapController = mapView.getController();
mapController.setZoom(17);
mapView.setBuiltInZoomControls(true);
// Add the MyLocationOverlay //
myLocation = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(myLocation);
myLocation.enableCompass();
myLocation.enableMyLocation();
// Animates the map to GPS Position //
myLocation.runOnFirstFix(new Runnable() {
@Override
public void run() {
mapController.animateTo(myLocation.getMyLocation());
}
});
}
@Override
protected boolean isRouteDisplayed() {
// Location Manager Intiation
locationManager = (LocationManager) statuspage.this
.getSystemService(LOCATION_SERVICE);
criteria = new Criteria();
// More accurate, GPS fix.
criteria.setAccuracy(Criteria.ACCURACY_FINE); // More accurate, GPS fix.
bestProvider = locationManager.getBestProvider(criteria, true);
location = locationManager.getLastKnownLocation(bestProvider);
updateWithNewLocation(location);
locationManager.requestLocationUpdates(bestProvider, 60000, 10,
locationListener); // 1800000 = 30 Min
return false;
}
class GeoCoder extends AsyncTask<Void, Void, Void> {
String lat = "Acquiring";
String lng = "Acquiring";
@Override
protected Void doInBackground(Void... params) {
if (location != null) {
/**
* double latitude = myLocation.getMyLocation().getLatitudeE6();
* double longitude =
* myLocation.getMyLocation().getLongitudeE6();
*/
double latitude = location.getLatitude();
double longitude = location.getLongitude();
lat = "" + latitude;
lng = "" + longitude;
// gc = new Geocoder(statuspage.this, Locale.getDefault());
Geocoder gc = new Geocoder(getApplicationContext(),
Locale.getDefault());
try {
List<Address> addresses = gc.getFromLocation(latitude,
longitude, 1);
sb = new StringBuilder();
if (addresses != null && addresses.size() > 0) {
address = addresses.get(0);
int noOfMaxAddressLine = address
.getMaxAddressLineIndex();
if (noOfMaxAddressLine > 0) {
for (int i = 0; i < address
.getMaxAddressLineIndex(); i++) {
sb.append(address.getAddressLine(i)).append(
"\n");
}
addressString = sb.toString();
}
}
} catch (Exception e) {
addressString = "Sorry, we are trying to find information about this location";
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
TextView scrollview = (TextView) findViewById(R.id.scrollview);
// Latitude and Longitude TextView
TextView etlongitude = (TextView) findViewById(R.id.etlongitude);
TextView etlatitude = (TextView) findViewById(R.id.etlatitude);
// TextView to display GeoCoder Address
scrollview.setGravity(Gravity.CENTER);
scrollview.setText("Your location:" + "\n"
+ "(Accurate to 500 meters)" + "\n" + (addressString));
Log.d("Address", (addressString));
// Latitude and Longitude TextView Display Coordinates //
etlongitude.setText(lng);
etlatitude.setText(lat);
// Log.d("GeoCoder", "In-Task");
return;
}
No comments:
Post a Comment