Broadcast Receiver and Toasting

470 Views Asked by At

All,

I have a broadcast receiver that does not have an associated layout or any activities. It is listening for some to unplug something from the audio jack.

I have been able to get my receiver to work and raise a notification. My question is how do I show a Toast message using my receiver that will not have a main activity or main layout?

Now I have found a post or two that basically says this cannot be done because you need a looper for a Toast to work. Android + SMS Receiver + Toast = FAIL

I am not familiar with loopers other than just this link http://developer.android.com/reference/android/os/Looper.html.

Is there a way to create a IntentService from the onReceive method of the broadcast receiver that has a looper on the worker thread to enable me to raise a Toast when my receiver is triggered?

1

There are 1 best solutions below

0
On BEST ANSWER

Think you will be gettign context in the onReceive method of broadcast receiver try the below code.

public class CallReceiver extends BroadcastReceiver {
   @Override
   public void onReceive(final Context context, final Intent intent) {
      Toast.makeText(context, "NEW_TOAST",Toast.LENGTH_LONG).show();
   }
}