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();
}
});
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.