How do you display a Toast from a background thread on Android?
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();
}
});
How to use toast message inside Thread in Android
You can not use or access any UI elements inside a non UI thread. If you want to see some messages about your thread then you may use AsyncTask and use the UI stuffs inside preExecute method.because the pre and postExecute of AsyncTask are run on UI thread.
If you want to do like so then see Problem with Toast in AsyncTask method call .
If you are excepting a small and easy way, then just use LOG class like:
Log.d("String Key", "the value you want to see");
If you want some ideas about log then please see Android Log.v(), Log.d(), Log.i(), Log.w(), Log.e() - When to use each one?
Hope these are sufficient for your question.
How to display a Toast in Android AsyncTask?
You cannot update UI on background thread. doInBackground()
is invoked on the background thread. You should update UI on the UI thread.
runOnUiThread(new Runnable(){
@Override
public void run(){
//update ui here
// display toast here
}
});
onPreExecute()
, onPostExecute(Result)
, are invoked on the UI thread. So you can display toast here.
onProgressUpdate(Progress...)
, invoked on the UI thread after a call to publishProgress(Progress...)
can be used to animate a progress bar or show logs in a text field.
The result of doInBackground()
computation is a parameter to onPostExecute(Result)
so return the result in doinBackground()
and show your toast in onPostExecute(Result)
You can also use a handler as suggested by @Stine Pike
For clarity, check the link below under the topic: The 4 steps.
http://developer.android.com/reference/android/os/AsyncTask.html
Show Toast in a Thread of a Service
You have to start the thread:
new Thread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),"Your message",Toast.LENGTH_LONG).show();
}
}).start();
How to display Toast from a Service after main Activity finishes?
OnHandleIntent
will run in a differant Thread
so you are showing Toast
in a thread which is not allowed in android
so change your code like this
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
getString(R.string.car_opened),
Toast.LENGTH_SHORT).show();
}
});
From this dead thread in service
IntentService will create a thread to handle the new intent, and terminated it immediately once the task has done. So, the Toast will be out of controlled by a dead thread.
You should see some exceptions in the console when the toast showing on the screen.
Null Pointer Exception after creating Toast in Fragment
It sounds like you are NOT calling the Toast.makeText()
from within the main thread. The thread that you call from needs access to the UI essentially for the Toast to display.
This SO thread might give you some pointers.
Related Topics
What Permission Do I Need to Access Internet from an Android Application
How to Install Google Play Services in a Genymotion Vm (With No Drag and Drop Support)
Android Gallery on Android 4.4 (Kitkat) Returns Different Uri For Intent.Action_Get_Content
Android Saving File to External Storage
How to Call Android Contacts List
Android - Dynamically Add Views into View
Passing a Bundle on Startactivity()
How to Add Dividers and Spaces Between Items in Recyclerview
Android: How to Get String from Resources Using Its Name
How to Use Recyclerview Inside Nestedscrollview
How to Use Drawerlayout to Display Over the Actionbar/Toolbar and Under the Status Bar
App Not Installed' Error on Android