0

I guess this is a question straight to Mark, but I can't for the life of me get a Toast notification to work from inside the doWakefulWork method of WakefulIntentService. It works from the onCreate, but I need to use some data I am getting through the intent within the toast message.

I've tried to instantiate the handler from within the OnCreate but this doesn't seem to do anything. I've tried everything I can think of. The Toast just doesn't seem to run on the main thread. Any help?

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
electrichead
  • 1,124
  • 8
  • 20

2 Answers2

0
       private boolean showToast = false;
        ...
        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (showToast) {
             Toast.makeText(this,"YourText",Toast.LENGTH_LONG).show();
             showToast = false;
        }
        }

Just set showToast = true; from anywhere to showing you toast notification.

iSun
  • 1,714
  • 6
  • 28
  • 57
0

In general, WakefulIntentService is not designed for scenarios where a Toast would make any sense, since the user may or may not be around. Moreover, services should not be directly affecting the UI this way, since if the user is around the user may not appreciate your Toast in the middle of their game, their navigation, their movie, etc. I would rather you use something else (e.g., ordered broadcast to update a foreground activity or display a Notification if there is no such activity).

If you are really certain that you want to use a Toast from a service, your problem should have nothing to do with WakefulIntentService -- you would have the same problems with a regular IntentService. This SO question has some answers demonstrating the use of a Toast from a service's background thread using a Handler, which may help indicate where your Handler implementation is going wrong.

Community
  • 1
  • 1
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • The Toast notification is actually a user preference setting, so the user has to ask for it in order for it to be shown. I have found a solution in the thread you linked to (from Marcos). – electrichead Mar 08 '12 at 19:22