CERCA
PER MODELLO
FullScreen Chatbox! :)

Utente del giorno: bluemask con ben 1 Thanks ricevuti nelle ultime 24 ore
Utente della settimana: megthebest con ben 6 Thanks ricevuti negli ultimi sette giorni
Utente del mese: megthebest con ben 18 Thanks ricevuti nell'ultimo mese

Visualizzazione dei risultati da 1 a 1 su 1
Discussione:

problema Handler e mancata assegnazione variabile

Se questa discussione ti è stata utile, ti preghiamo di lasciare un messaggio di feedback in modo che possa essere preziosa in futuro anche per altri utenti come te!
  1. #1
    Androidiano


    Registrato dal
    Aug 2010
    Messaggi
    87

    Ringraziamenti
    2
    Ringraziato 0 volte in 0 Posts
    Predefinito

    problema Handler e mancata assegnazione variabile

    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à

    x
     
    1
    public class LocationAddress {
    2
    3
        private static final String TAG = "LocationAddress";
    4
    5
        public static void getAddressFromLocation(final double latitude, final double longitude,
    6
                                                  final Context context, final Handler handler) {
    7
            Thread thread = new Thread() {
    8
                   @<a rel="nofollow" href="https://www.androidiani.com/forum/members/override.html" target="_blank">Override</a>
    9
                public void run() {
    10
                    Geocoder geocoder = new Geocoder(context, Locale.getDefault());
    11
                    String result = null;
    12
                    try {
    13
                        List<Address> addressList = geocoder.getFromLocation(latitude, longitude, 1);
    14
    15
                        if (addressList != null && addressList.size() > 0) {
    16
                            Address address = addressList.get(0);
    17
                            StringBuilder sb = new StringBuilder();
    18
                            for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
    19
                                sb.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();
    25
                            result = address.getLocality();
    26
                        }
    27
                    } catch (IOException e) {
    28
                        Log.e(TAG, "Unable connect to Geocoder", e);
    29
                    } finally {
    30
                        Message message = Message.obtain();
    31
                        message.setTarget(handler);
    32
                        if (result != null) {
    33
                            message.what = 1;
    34
                            Bundle bundle = new Bundle();
    35
                            //result = "Latitude: " + latitude + " Longitude: " + longitude +"\n\nAddress:\n" + result;
    36
                            bundle.putString("address", result);
    37
                            message.setData(bundle);
    38
                        } else {
    39
                            message.what = 1;
    40
                            Bundle bundle = new Bundle();
    41
                            //result = "Latitude: " + latitude + " Longitude: " + longitude +"\n Unable to get address for this lat-long.";
    42
                            bundle.putString("address", result);
    43
                            message.setData(bundle);
    44
                        }
    45
                        message.sendToTarget();
    46
                    }
    47
                }
    48
            };
    49
            thread.start();
    50
        }
    51
    }


    Questo runnable è associato ad un alert dialog,

    33
     
    1
    final Runnable runnable = new Runnable() {
    2
               @<a rel="nofollow" href="https://www.androidiani.com/forum/members/override.html" target="_blank">Override</a>
    3
            public void run() {
    4
                if (alert.isShowing()) {
    5
                    alert.dismiss();
    6
                    actualStar = (int) rating.getRating();
    7
                    rating.setRating(0); //ripristino il valore delle stelle
    8
    9
                    if (actualTag.equalsIgnoreCase("")) {
    10
                        actualTag = NOCATEGORIA;
    11
                        copyActualTag = actualTag;
    12
                        actualTag = "";
    13
                    } else {
    14
                        copyActualTag = actualTag;
    15
                        actualTag = "";
    16
                    }
    17
                    // new GetLocation().execute();
    18
                    getLocation();
    19
    20
                    //prova per testare il funzionamento del DB
    21
                    db.open();
    22
                    Cursor cursor = db.getPhotosTo(LocalitaArray.get(0), true);
    23
                    cursor.moveToNext();
    24
                    Log.i("Id", cursor.getString(0));
    25
                    Log.i("Path", cursor.getString(1));
    26
                    Log.i("Tag", cursor.getString(2));
    27
                    Log.i("Star", cursor.getString(3));
    28
                    Log.i("locality", cursor.getString(4));
    29
    30
                    onResume();
    31
                }
    32
            }
    33
        };


    Questo è il metodo getLocation()

    11
     
    1
     private void getLocation() {
    2
    3
            Location location = gps.getLocation(LocationManager.GPS_PROVIDER);
    4
    5
            if (location != null) {
    6
                double latitude = location.getLatitude();
    7
                double longitude = location.getLongitude();
    8
                LocationAddress locationAddress = new LocationAddress();
    9
                locationAddress.getAddressFromLocation(latitude, longitude, mContext, new GeocoderHandler());
    10
            } else showGpsSettingsAlert();
    11
        }


    Questa è la classe handler

    17
     
    1
      private class GeocoderHandler extends Handler {
    2
              @<a rel="nofollow" href="https://www.androidiani.com/forum/members/override.html" target="_blank">Override</a>
    3
            public void handleMessage(Message message) {
    4
                String locationAddress;
    5
                switch (message.what) {
    6
                    case 1:
    7
                        Bundle bundle = message.getData();
    8
                        locationAddress = bundle.getString("address");
    9
                        break;
    10
                    default:
    11
                        locationAddress = null;
    12
                }
    13
                locality = locationAddress;
    14
                localita.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?
    Ultima modifica di giupardeb; 04-08-15 alle 10:59 - Motivo: Aggiornamento

  2.  

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire risposte
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Torna su
Privacy Policy