Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create toast from IntentService? It gets stuck on the screen

Tags:

I'm trying to have my IntentService show a Toast message, but when sending it from the onHandleIntent message, the toast shows but gets stuck and the screen and never leaved. I'm guessing its because the onHandleIntent method does not happen on the main service thread, but how can I move it?

Has anyone has this issue and solved it?

like image 203
Omri Avatar asked Oct 17 '10 21:10

Omri


People also ask

How do you display the toast message at the center of the app?

A standard toast notification appears near the bottom of the screen, centered horizontally. You can change this position with the setGravity(int, int, int) method. This accepts three parameters: a Gravity constant, an x-position offset, and a y-position offset.

Can I show toast from background thread?

Steps: declare a handler in the main activity (onCreate) now create a thread from the main activity and pass the Context of that activity. now use this context in the Toast, instead of using getApplicationContext()

Which method is used to create a toast in Android?

The Toast. makeText() method is a pre-defined method which creates a Toast object. Parameters: This method accepts three parameters: context: The first parameter is a Context object which is obtained by calling getApplicationContext().

How do I show a toast at a specific time in Android?

In general, a Toast can be displayed for either 2 seconds (Toast. LENGTH_SHORT) or 3.5 seconds (Toast. LENGTH_LONG). In this article, we will show you how you could display Toast for longer or shorter in Android.


2 Answers

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"));  } 
like image 169
Nathan Schwermann Avatar answered Sep 21 '22 12:09

Nathan Schwermann


Here is the full IntentService Class code demonstrating Toasts that helped me:

package mypackage;  import android.app.IntentService; import android.content.Intent; import android.os.Handler; import android.os.Looper; import android.widget.Toast;  public class MyService extends IntentService {     public MyService() { super("MyService"); }      public void showToast(String message) {         final String msg = message;         new Handler(Looper.getMainLooper()).post(new Runnable() {             @Override             public void run() {                 Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();             }         });     }      @Override     protected void onHandleIntent(Intent intent) {         showToast("MyService is handling intent.");     } } 
like image 25
Mike Shiyan Avatar answered Sep 22 '22 12:09

Mike Shiyan