29

I have an IntentService that downloads some files. The problem is that I create a Toast inside the IntentService like this

Toast.makeText(getApplicationContext(), "some message", Toast.LENGTH_SHORT).show();

The Toast will never disappear event if I exit the app. The only way to destroy it is to kill the process.

What am I doing wrong?

jax
  • 37,735
  • 57
  • 182
  • 278
  • There's no reason not to create a Toast from a service. From [the notification dev guide](http://developer.android.com/guide/topics/ui/notifiers/toasts.html): >If you create a toast notification from a Service, it appears in front of the Activity currently in focus. – Pierre-Luc Paour Oct 11 '10 at 12:06
  • That must be wrong, because it doesn't work when you test it with IntentService. @rony's solution works best. – IgorGanapolsky Oct 04 '12 at 15:19

6 Answers6

75

The problem is that IntentService is not running on the main application thread. you need to obtain a Handler for the main thread (in onCreate()) and post the Toast to it as a Runnable.

the following code should do the trick:

@Override
public void onCreate() {
    super.onCreate();
    mHandler = new Handler();
}

@Override
protected void onHandleIntent(Intent intent) {
    mHandler.post(new Runnable() {            
        @Override
        public void run() {
            Toast.makeText(MyIntentService.this, "Hello Toast!", Toast.LENGTH_LONG).show();                
        }
    });
}
maysi
  • 5,457
  • 12
  • 34
  • 62
rony l
  • 5,798
  • 5
  • 35
  • 56
24

This works for me:

public void ShowToastInIntentService(final String sText) {
    final Context MyContext = this;

    new Handler(Looper.getMainLooper()).post(new Runnable() {
        @Override
        public void run() {
            Toast toast1 = Toast.makeText(MyContext, sText, Toast.LENGTH_LONG);
            toast1.show();
        }
    });
};
Daniel
  • 209
  • 1
  • 3
  • 12
Ton
  • 9,235
  • 15
  • 59
  • 103
8

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.

bluesway
  • 107
  • 1
  • 3
1

For people developing in Xamarin studio, this is how its done there:

Handler handler = new Handler ();
handler.Post (() => {
    Toast.MakeText (_Context, "Your text here.", ToastLength.Short).Show ();
});
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Bugs Happen
  • 2,169
  • 4
  • 33
  • 59
0

To show a toast when the user is in one of the application activity.

Just need a reference of the current activity, and call it with this sample code:

public void showToast(final String msg) {
    final Activity a = currentActivity;
    if (a != null ) {
        a.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                Toast.makeText(a, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

There is a lot of options to get the current activity, check this question: How to get current foreground activity context in android?

But I use this approach:

The application must have:

private Activity currentActivity = null;

public Activity getCurrentActivity() {
    return currentActivity;
}

public void setCurrentActivity(Activity mCurrentActivity) {
    this.currentActivity = mCurrentActivity;
}

Each activity must have:

@Override
protected void onResume() {
    super.onResume();
    ((MyApplication) getApplication()).setCurrentActivity(this);

}
@Override
protected void onPause() {
    super.onPause();
    ((MyApplication) getApplication()).setCurrentActivity(null);
}
Community
  • 1
  • 1
Daniel De León
  • 13,196
  • 5
  • 87
  • 72
-9

You shouldn't create Toasts from a Service. You should use a Notification instead.

Rich Schuler
  • 41,814
  • 6
  • 72
  • 59
  • 13
    Wrong. from http://developer.android.com/guide/topics/ui/notifiers/toasts.html "A toast can be created and displayed from an Activity or Service". Its not working because "Service" runs on Main thread, whereas "Intent service" run on another thread. – Palani Oct 20 '11 at 10:43
  • 6
    Just because you can do a thing doesn't mean you should do a thing. You shouldn't create toasts from a service. – Rich Schuler Oct 26 '11 at 07:47
  • @Qberticus, can you update your answer with a code example of how Notification would be used in this case? – ban-geoengineering May 09 '14 at 17:33
  • 1
    Why should we use `Notification`s instead of `Toast`s from services? Is it because of a technical limitation? Is it because the user mightn't be looking at the screen? – Sam Nov 05 '14 at 10:21