Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7643923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:33:53+00:00 2026-05-31T09:33:53+00:00

I try to send a Toast Message from a Thread to the UIThread when

  • 0

I try to send a Toast Message from a Thread to the UIThread when clicking a button.
However, every time I click the button the Toast does not appear.
I am using a Handler to do this.

This is the full code, in case I made a major 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: Solved it by using this:

Handler toastHandler = new Handler();
Runnable toastRunnable = new Runnable() {public void run() {Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();}};

In the UIThread and this:

toastHandler.post(toastRunnable);

in the background thread.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:33:54+00:00Added an answer on May 31, 2026 at 9:33 am

    Try this:

    runOnUiThread(new Runnable()
    {
        public void run()
        {
            // Here you can make a Toast
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to send a message from to global page to my injected.js on
When i try to send a message from my client , the server is
i try to send some data to my php file from ajax , but
I try to send json array to ajax (from PHP to jQuery) in my
I try to send a sms using next code, but recipient gets my message
I try send wap push message. I set datacoding 0xf5 and send submit sm
I am trying to send a message to my main activity from an Async
Does every device send the BOOT_COMPLETED? I want to start an Activity on Boot
I try to send gps longitude and latitude to the android emulator with eclipse,
I try to send a request to my server via GET, but qooxdoo sends

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.