Buongiorno ragazzi, da circa due giorni sto impazzendo con questo problema. Vorrei che la mia applicazione recuperi le coordinate gps e determini la località in cui mi trovo, fatto ciò deve salvare questa località in un'altra variabile e all'interno di un arrayLIst. Ecco il codice:
LocationAddress, usata per determinare la località
x1public class LocationAddress {
2
3private static final String TAG = "LocationAddress";
4
5public static void getAddressFromLocation(final double latitude, final double longitude,
6final Context context, final Handler handler) {
7Thread thread = new Thread() {
8<a rel="nofollow" href="https://www.androidiani.com/forum/members/override.html" target="_blank">Override</a>
9public void run() {
10Geocoder geocoder = new Geocoder(context, Locale.getDefault());
11String result = null;
12try {
13List<Address> addressList = geocoder.getFromLocation(latitude, longitude, 1);
14
15if (addressList != null && addressList.size() > 0) {
16Address address = addressList.get(0);
17StringBuilder sb = new StringBuilder();
18for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
19sb.append(address.getAddressLine(i)).append("\n");
20}
21// sb.append(address.getLocality()).append("\n");
22// sb.append(address.getPostalCode()).append("\n");
23// sb.append(address.getCountryName());
24// result = sb.toString();
25result = address.getLocality();
26}
27} catch (IOException e) {
28Log.e(TAG, "Unable connect to Geocoder", e);
29} finally {
30Message message = Message.obtain();
31message.setTarget(handler);
32if (result != null) {
33message.what = 1;
34Bundle bundle = new Bundle();
35//result = "Latitude: " + latitude + " Longitude: " + longitude +"\n\nAddress:\n" + result;
36bundle.putString("address", result);
37message.setData(bundle);
38} else {
39message.what = 1;
40Bundle bundle = new Bundle();
41//result = "Latitude: " + latitude + " Longitude: " + longitude +"\n Unable to get address for this lat-long.";
42bundle.putString("address", result);
43message.setData(bundle);
44}
45message.sendToTarget();
46}
47}
48};
49thread.start();
50}
51}
Questo runnable è associato ad un alert dialog,
331final Runnable runnable = new Runnable() {
2<a rel="nofollow" href="https://www.androidiani.com/forum/members/override.html" target="_blank">Override</a>
3public void run() {
4if (alert.isShowing()) {
5alert.dismiss();
6actualStar = (int) rating.getRating();
7rating.setRating(0); //ripristino il valore delle stelle
8
9if (actualTag.equalsIgnoreCase("")) {
10actualTag = NOCATEGORIA;
11copyActualTag = actualTag;
12actualTag = "";
13} else {
14copyActualTag = actualTag;
15actualTag = "";
16}
17// new GetLocation().execute();
18getLocation();
19
20//prova per testare il funzionamento del DB
21db.open();
22Cursor cursor = db.getPhotosTo(LocalitaArray.get(0), true);
23cursor.moveToNext();
24Log.i("Id", cursor.getString(0));
25Log.i("Path", cursor.getString(1));
26Log.i("Tag", cursor.getString(2));
27Log.i("Star", cursor.getString(3));
28Log.i("locality", cursor.getString(4));
29
30onResume();
31}
32}
33};
Questo è il metodo getLocation()
111private void getLocation() {
2
3Location location = gps.getLocation(LocationManager.GPS_PROVIDER);
4
5if (location != null) {
6double latitude = location.getLatitude();
7double longitude = location.getLongitude();
8LocationAddress locationAddress = new LocationAddress();
9locationAddress.getAddressFromLocation(latitude, longitude, mContext, new GeocoderHandler());
10} else showGpsSettingsAlert();
11}
Questa è la classe handler
171private class GeocoderHandler extends Handler {
2<a rel="nofollow" href="https://www.androidiani.com/forum/members/override.html" target="_blank">Override</a>
3public void handleMessage(Message message) {
4String locationAddress;
5switch (message.what) {
6case 1:
7Bundle bundle = message.getData();
8locationAddress = bundle.getString("address");
9break;
10default:
11locationAddress = null;
12}
13locality = locationAddress;
14localita.add(locality);
15
16}
17}
Quando vado a fare il debug, il mio arrayLIst risulta di dimensione zero, e la variabile locality è vuota. Cosa sto sbagliando? Spero di essere stato chiaro, buona giornata
[UPDATE1] Ho notato che l'applicazione si blocca ancora prima che il thread che determina la località venga terminato. COme posso far in modo che il thread principale dell'applicazione aspetti che il thread che determina la località finisca?