显示来自 BroadcastReceiver 的复杂 Toast
我想知道是否有人可以帮助我。我试图在收到短信时显示 toast 元素。这个 toast 应该包含一个布局,其中有一个图像(短信图标)和 2 个文本视图(发件人、消息)
如果我从活动中调用以下方法,它会按预期工作...
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();
}
但是,如果我尝试在以同样的方式从我的短信接收器中,我得到:
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
有人可以告诉我如何从意图中做到这一点吗?我认为这个问题在某种程度上与跨线程有关,但是我不确定如何继续。我在网上看到了几个示例,但它们似乎要么使用已弃用的代码,要么只显示简单的文本
有人可以指出我正确的方向吗
非常感谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 LayoutInflater.from(context) 来获取您的 LayoutInflater。像这样:
You can use LayoutInflater.from(context) to get your LayoutInflater. Like this:
您的编译错误是因为
BroadcastReceiver
不是从Context
继承的。使用传递给onReceive()
的Context
(并摆脱getApplicationContext()
- 只需使用Context
代码>你通过了)。现在,这可能仍然不起作用,因为我不确定您是否可以首先从
BroadcastReceiver
引发Toast
,但是这个至少会让你克服编译错误。Your compile errors are because
BroadcastReceiver
does not inherit fromContext
. Use theContext
that is passed intoonReceive()
(and get rid ofgetApplicationContext()
-- just use theContext
you are passed).Now, this may still not work, as I am not sure if you can raise a
Toast
from aBroadcastReceiver
in the first place, but this will at least get you past the compile errors.可以从活动或服务(而不是广播接收器)创建和显示 toast
A toast can be created and displayed from an Activity or Service, not Broadcast receiver