Je craignais que trouver des informations de localisation (avec le géocodage inverse) sur le thread principal ne ralentisse mon interface utilisateur. Pour résoudre ce problème, j'ai mis l'info dans un AsyncTask (code ci-dessous) Maintenant, je veux ajouter des écouteurs de localisation, j'ai toutes sortes de problèmes. J'ai fait un peu de recherche et maintenant je me demande ... Est-il même nécessaire de mettre le code de localisation dans un AsyncTask? Peut-être que Android "naturellement" trouve des informations de localisation de manière asynchrone?Est-il nécessaire de trouver des emplacements dans un fil séparé?
public class LocationAsyncTask extends AsyncTask<Void, String, String> {
@Override protected void onPostExecute(String result) {
// TODO Auto-generated method stub
tvLocation.setText(result);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
tvLocation.setText("Finding current location");
}
@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);
Location location =locationManager.getLastKnownLocation(provider);
String strUpdateResult = updateWithANewLocation(location);
//locationManager.requestLocationUpdates(provider, 1000, 10, locationListener);
return strUpdateResult;
}
private String updateWithANewLocation(Location location) {
// TODO Auto-generated method stub
StringBuilder sbLocation = new StringBuilder();
String strLocation = new String();
TextView myLocationText;
String addressString = "No address found";
String latLongString = "";
//If there is a location
if (location!= null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
double altitude = location.getAltitude();
float accuracy = location.getAccuracy();
float bearing = location.getBearing();
long time = location.getTime();
float speed = location.getSpeed();
String prov = location.getProvider();
strLocation = "Latitude: " + latitude + "\nLongitude: " + longitude + "\n" ;
publishProgress(strLocation);
Geocoder gc = new Geocoder(LateRunner.this, Locale.getDefault());
try {
List<Address> addresses = gc.getFromLocation(latitude, longitude, 1);
//if location and then address is found
if (addresses.size() > 0) {
Address address = addresses.get(0);
for (int i=0; i <address.getMaxAddressLineIndex(); i++) {
sbLocation.append(address.getAddressLine(i)).append("\n");
}
strLocation= sbLocation.toString();
publishProgress(strLocation);
}
//if location but no address
else {
strLocation = "Latitude: " + latitude + "\nLongitude: " + longitude + "\n";
publishProgress(strLocation);
} //end try
} catch (IOException e) {
strLocation = "Latitude: " + latitude + "\nLongitude: " + longitude + "\n";
publishProgress(strLocation);
}//end catch
}
//If no location found
else {
strLocation = "Unable to find location. ";
publishProgress(strLocation);
}
return strLocation;
}// end updateWithANewLocation()
@Override protected void onProgressUpdate(String... result) {
// TODO Auto-generated method stub
tvLocation.setText(result[0]);
}
}
Pourriez-vous spécifier certaines de ces "toutes sortes de problèmes"? – MByD
Je ne trouvais nulle part où placer l'emplacement locationListener (peu importe où je l'ai mis, par exemple, en le faisant un membre de l'AsyncTask, en déplaçant sur onCreate provoque le crash du périphérique). C'est pourquoi je voudrais abandonner l'AsyncTask tous ensemble si ce n'est pas nécessaire. – Mel