How to create toast from IntentService? It gets stuck on the screen
in onCreate()
initialize a Handler
and then post to it from your thread.
private class DisplayToast implements Runnable{
String mText;
public DisplayToast(String text){
mText = text;
}
public void run(){
Toast.makeText(mContext, mText, Toast.LENGTH_SHORT).show();
}
}
protected void onHandleIntent(Intent intent){
...
mHandler.post(new DisplayToast("did something"));
}
Android Service to show toast
See this part of the docs
(An IntentService has a few limitations:
It can't interact directly with your user interface. To put its
results in the UI, you have to send them to an Activity.
You need to put it on the main Thread
. See the answer here by rony of a way to do that.
and from the full documentation on IntentService
handles each Intent in turn using a worker thread
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.
Android Toast Won't Disappear
The only explanation is that your Toast
is called in a loop. You should track the toast .show()
and see if it is not called an infinite times.
An other visual way would be to do this
Toast.makeText(SugarLoafContext.playbackTabContext, "No clips found.", duration).show();
Toast.makeText(SugarLoafContext.playbackTabContext, "Do you understand now?", duration).show();
I am sure you will see both toast alternatively during a looong time...
Dialog: MyActivity has stopped
Your service might be in a different thread.
Use handler to show Toast:
Handler handler = new Handler();
Runnable toastRunnable = new Runnable(){
@Override public void run() {
Toast.makeText(MyService.this, "onCreate: Service work!", Toast.LENGTH_SHORT).show();
}
};
handler.post(toastRunnable);
How to show a toast from a worker thread inside a service?
First of all, you really shouldn't send Toast messages from a Service or from any task that wasn't initiated by the user a very short time ago. Otherwise the user won't know where the Toast is originating from; she can't even be sure from which app it comes - absolutely confusing and bad practice in general.
But to answer your question, when you create a Handler with new Handler()
then it is attached to the thread it was created on. Thus, if you want to post something to the UI/main thread from a service (from any thread) then you should create the Handler in, for instance, the onCreate()
method; because onCreate()
, and usually any Android callback method, is called on the UI thread. So:
public class MyService extends Service {
private Handler mMainThreadHandler;
@Override
public void onCreate() {
mMainThreadHandler = new Handler();
}
[...]
}
Or, of you want to create a Handler that is attached to the UI thread and don't want to care about where it is created then use new Handler(Looper.getMainLooper())
. This way it is attached to the main looper; which is the looper of the main thread.
public class MyService extends Service {
private Handler mMainThreadHandler = new Handler(Looper.getMainLooper());
[...]
}
I know you wrote you tried it; but it should work.
Though, what I would recommend you is not to bother with Handlers; use Needle instead. Needle is a simple and powerful multithreading library for Android. With it, you can say things like:
Needle.onMainThread().execute(new Runnable() {
@Override
public void run() {
// e.g. change one of the views
}
});
Check it out on GitHub: https://github.com/ZsoltSafrany/needle
Related Topics
Java.Lang.Numberformatexception: Invalid Int: "" in Android
Material Design, Appcompat, and Backwards Compatibility
How to Track Time in Libgdx(Android)
Why Getapplicationcontext() in Constructor of Activity Throws Null Pointer Exception
How to Attach Image from Drawable to Gmail
Kotlin and Firebase Read and Write Data
Mpandroid Chart Not Displaying Any Labels for Xaxis , What Is Missing
How to Customize Listview Row Android
Setpluginsenabled Not Exist for Webview
Text/Design Tab Missing New Android Project on Android Studio Ide
Action Buttons Doesn't Show Up on Action Bar
Move Up Text View Only When Virtual Keyboard Open on Android
How to Implement Autocompletetextview with Listview
Sms Manager Send Mutlipart Message When There Is Less Than 160 Characters
How to Connect SQL Server Using Jtds Driver in Android