1

My application has many classes, worker threads, and background service operations. Notifications can come in from those other threads and they can come in on a service when the app isn't running. I have a utility class where I would like to write a single "SendToast(Context ctx, String message)" method that can handle all of these situations. Is it possible? This SO post comes close, but it won't work for my service messages

// won't work.. I need something that can run given a Context, rather than
// an Activity
public static void ShowToast(final Activity activity, 
    final String message, int length) {
       activity.runOnUiThread(new Runnable() {
       public void run() {
       Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
    }
});
Community
  • 1
  • 1
MrGibbage
  • 2,644
  • 5
  • 39
  • 50

1 Answers1

1

To quote from the Android Design Patterns site: "Dialogs and toasts are for feedback not notification. Your app should not create a dialog or toast if it is not currently on screen".

See: http://developer.android.com/design/patterns/notifications.html

Use a Notification instead.

Joseph Earl
  • 23,351
  • 11
  • 76
  • 89
  • OK. Good point. I could do that, but I thought it would be nice to have toasts come up just in case the user was looking at the screen when the message came in. They are a little less intrusive than task-bar notifications, and in some cases that may be preferable. Any chance it could still be done (even though the android big-wigs say not to), or is it just a bad idea and I should give it up? – MrGibbage Apr 07 '12 at 01:08