显示复杂的吐司从BroadcastReceiver的吐司、复杂、BroadcastReceiver

2023-09-05 08:35:42 作者:≮掵終紸顁≯

我不知道是否有人能帮助我。我想在收到短信时显示敬酒元素。这敬酒应包含一个布局具有图像(SMS图标)和2 textviews(发件人,邮件)

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

不过,如果我尝试从我SMSReceiver调用相同的code以同样的方式,我得到:

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

有人可以请告知我怎么可以从一个意图做tihs。我认为这个问题在某种程度上涉及到跨线程但是,我不能确定如何进行。我见过一些例子在线,但他们似乎无论是使用德precated code或只显示简单的文本

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

感谢

推荐答案

您可以使用LayoutInflater.from(上下文),让您的LayoutInflater。像这样的:

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);