I am trying to send a Toast message from a thread to UIThread when a button is clicked. However, every time I press the button, the toast does not appear. I use a handler for this.
This is the complete code if I made a big mistake somewhere:
package google.map.activity; //imports public class GoogleMapActivity extends MapActivity { int lat = 0; int lng = 0; Location location; MapController mc; GeoPoint p; private MapController mapController; private MapView mapView; private LocationManager locationManager; // //////////////////////////////////////////////////////////////////////////////////////////////////////// /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.googlemapactivity); mapView = (MapView) findViewById(R.id.mapView); mapView.setBuiltInZoomControls(false); mapView.setSatellite(false); mapController = mapView.getController(); mapController.setZoom(19); locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5, 0, new GeoUpdateHandler()); locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 5, 0, new GeoUpdateHandler()); // //////---Switch to // Start///////////////////////////////////////////////////////////////////////////// Button switchToMain = (Button) findViewById(R.id.switchtomain); switchToMain.setOnClickListener(new View.OnClickListener() { @Override public void onClick(final View view) { final Intent intent = new Intent(); setResult(RESULT_OK, intent); finish(); } }); // ////--Call a // Cab///////////////////////////////////////////////////////////////////////////////////// Button getCab = (Button) findViewById(R.id.GetCab); // create the Button final Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); getCab.setOnClickListener(new View.OnClickListener() { String bestProvider = locationManager.getBestProvider(criteria, true); Location locationTest = locationManager .getLastKnownLocation(bestProvider); // get // last // known // location fix from // locationManager public void getAddress() { String msg = null; Looper.prepare(); if (locationTest == null) { bestProvider = LocationManager.NETWORK_PROVIDER; } Location location = locationManager .getLastKnownLocation(bestProvider); Geocoder geocoder = new Geocoder(getBaseContext(), Locale .getDefault());// create // new // GeoCoder String result = null; // initialize result try { // try to get Address list from location of bestProvider List<Address> list = geocoder.getFromLocation( location.getLatitude(), location.getLongitude(), 1); if (list != null && list.size() > 0) { Address address = list.get(0); // sending back first address line and locality result = address.getAddressLine(0) + ", " + address.getLocality();// set result // to // Streetname+Nr. // and City } } catch (IOException e) { msg = String.format("IOException!!"); } finally { if (result != null) { msg = String.format(result); // Looper.loop(); } else msg = String.format("Keine Position"); } Message myMessage = new Message(); Bundle resBundle = new Bundle(); resBundle.putString("status", msg); myMessage.obj = resBundle; handler.sendMessage(myMessage); } @Override public void onClick(View v) { new Thread(new Runnable() { public void run() { // Looper.myLooper(); // Looper.prepare(); getAddress(); // get the Address Location locationTest = locationManager .getLastKnownLocation(bestProvider); if (locationTest == null) { bestProvider = LocationManager.NETWORK_PROVIDER; } Location location2 = locationManager .getLastKnownLocation(bestProvider); int lat = (int) (location2.getLatitude() * 1E6); // get // position // for GeoPoint int lng = (int) (location2.getLongitude() * 1E6); GeoPoint point = new GeoPoint(lat, lng); // create // GeoPoint // for // mapController mapController.animateTo(point); } }).start(); // toast.show(); } }); } public Handler handler = new Handler() { @Override public void handleMessage(Message msg) { Toast.makeText(getApplicationContext(), msg.toString(), Toast.LENGTH_LONG); } }; public class GeoUpdateHandler implements LocationListener { @Override public void onLocationChanged(Location location) { int lat = (int) (location.getLatitude() * 1E6); int lng = (int) (location.getLongitude() * 1E6); GeoPoint point = new GeoPoint(lat, lng); mapController.animateTo(point); // mapController.setCenter(point); } @Override public void onProviderDisabled(String provider) { } @Override public void onProviderEnabled(String provider) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } }
Thank you in advance!
Edit: resolved it using the following:
Handler toastHandler = new Handler(); Runnable toastRunnable = new Runnable() {public void run() {Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();}};
In UIThread and this:
toastHandler.post(toastRunnable);
in the background thread.
source share