Displaying toast requires it be done in the main UI thread. The following code is an example of a static method that can be executed from any thread (including a background Service when your application isn't even in the foreground).
public class ServiceUtils {
//-------------------------------------------------------------------------
// Constructor
// All methods in this class are static, no need for a public constructor
private ServiceUtils() {}
private static final Handler s_toastHandler = new Handler(Looper.getMainLooper());
public static void notifyUI(final Context context, final String toastMsg) {
s_toastHandler.post(new Runnable() {
public void run() {
try {
Toast.makeText(context,
toastMsg,
Toast.LENGTH_SHORT).show();
}
catch(Exception ex) {
Log.e(ServiceUtils.class.getSimpleName(), ex.getMessage());
}
}
});
}
}
Now you can call from anywhere:
ServiceUtils.notifyUI(getApplicationContext(), "some toast message")