Asked  7 Months ago    Answers:  5   Viewed   54 times

How can I display Toast messages from a thread?



You can do it by calling an Activity's runOnUiThread method from your thread:

activity.runOnUiThread(new Runnable() {
    public void run() {
        Toast.makeText(activity, "Hello", Toast.LENGTH_SHORT).show();
Tuesday, June 1, 2021
answered 7 Months ago

Matthieu's solution will work fine for most, but some can face problem; unless digging in many links provided here or from web, like Anders Göransson's explanation. I am trying to summarize some other reads right here and quickly explain solution if executeOnExecutor is still working in single thread...

Behavior of AsyncTask().execute(); has changed through Android versions. Before Donut (Android:1.6 API:4) tasks were executed serially, from Donut to Gingerbread (Android:2.3 API:9) tasks executed paralleled; since Honeycomb (Android:3.0 API:11) execution was switched back to sequential; a new method AsyncTask().executeOnExecutor(Executor) however, was added for parallel execution.

In sequential processing all Async tasks run in a single thread and thus have to wait before the previous task ends. If you need to execute code immediately, you need tasks to be processed in parallel in separate threads.

With AsyncTask serial execution is not available between Donut and Honeycomb versions, while parallel execution is not available before Donut.

For parallel processing after Donut: Check the Build version and based on that use .execute() or .executeOnExecutor() method. Following code can help...

AsyncTask<Void,Void,Void> myTask = new AsyncTask<Void,Void,Void>() { ... }; // ... your AsyncTask code goes here

NOTE: Function .executeOnExecutor() has checks if targetSdkVersion of project is less than or equal to HONEYCOMB_MR1 (Android:2.1 API:7) then it forces the executor to be THREAD_POOL_EXECUTOR (which runs Tasks sequentially in post Honeycomb).
If you have not defined a targetSdkVersion then minSdkVersion is automatically considered to be the targetSdkVersion.
Hence for running your AsyncTask in parallel on post Honeycomb you cannot leave targetSdkVersion empty.

Tuesday, June 8, 2021
answered 6 Months ago

Are you forgetting Toast#show?

Toast toast = Toast.makeText(context, "sometext", Toast.LENGTH_LONG);;
Friday, August 6, 2021
answered 4 Months ago


  runOnUiThread(new Runnable() {
                 public void run() {


after this line

  Log.d("ClientActivity", "C: Connecting...");
Thursday, September 30, 2021
answered 2 Months ago

You can't launch the Activity from your Service and get an immediate response. The response will need to come back asynchronously. There are several ways to do this:

  1. Your Activity can call startService() with the response as an extra in the Intent. This will result in onStartCommand() being called in the Service.

  2. The Activity can send a local broadcast Intent containing the response. In that case the Service needs to set up a BroadcastReceiver to listen for the response.

  3. Activity can bind to the Service and pass the data back as @njzk2 suggested (this is a bit more complicated though).

Monday, December 6, 2021
answered 1 Day ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :