5

I wonder if someone can help me. I'm trying to display a toast element when an SMS is received. This toast should contain a layout which has an image (SMS Icon) and 2 textviews (sender, message)

If I call the following method from an activity, it works as expected...

public void showToast(Context context, String name, String message) {
    LayoutInflater inflater = getLayoutInflater();
    View layout = inflater.inflate(R.layout.toast_sms,
                                   (ViewGroup) findViewById(R.id.toast_sms_root));

    TextView text = (TextView) layout.findViewById(R.id.toastsms_text);
    text.setText(message);

    Toast toast = new Toast(getApplicationContext());
    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
    toast.setDuration(Toast.LENGTH_LONG);
    toast.setView(layout);
    toast.show();
}

However, if I try to call the same code in the same way from my SMSReceiver, I get:

The method getLayoutInflater() is undefined for the type SmsReceiver
The method findViewById(int) is undefined for the type SmsReceiver
The method getApplicationContext() is undefined for the type SmsReceiver

Can someone please advise how I can do tihs from an intent. I assume the issue is somehow related to cross-threading however, I'm unsure how to proceed. I've seen a couple of examples online but they seem to either use deprecated code or only display simple text

Can someone please point me in the correct direction

Many thanks

1
  • Any reason/constructive criticism for the downvote?
    – Basic
    Aug 5, 2013 at 16:28

3 Answers 3

11

You can use LayoutInflater.from(context) to get your LayoutInflater. Like this:

LayoutInflater mInflater = LayoutInflater.from(context);
View myView = mInflater.inflate(R.layout.customToast, null);
TextView sender = (TextView) myView.findViewById(R.id.sender);
TextView message = (TextView) myView.findViewById(R.id.message);
0
7

Your compile errors are because BroadcastReceiver does not inherit from Context. Use the Context that is passed into onReceive() (and get rid of getApplicationContext() -- just use the Context you are passed).

Now, this may still not work, as I am not sure if you can raise a Toast from a BroadcastReceiver in the first place, but this will at least get you past the compile errors.

2
  • Ok, I'm now doing new Toast(context) but it hasn't resolved th other 2 compile errors - getLayoutInflater and findViewById. How can I reference/instantiate these?
    – Basic
    Oct 24, 2009 at 21:52
  • findViewById() only exists in Activity. Pass in null as your second parameter to the layout() call. To get a LayoutInflater, see the documentation for LayoutInflater in the SDK. Oct 24, 2009 at 22:11
1

A toast can be created and displayed from an Activity or Service, not Broadcast receiver

1
  • 1
    No, a toast can be displayed from a broadcast receiver, it worked for me, so it is possible. Oct 2, 2013 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.