显示来自 BroadcastReceiver 的复杂 Toast

发布于 2024-08-08 15:27:00 字数 1111 浏览 11 评论 0原文

我想知道是否有人可以帮助我。我试图在收到短信时显示 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

微凉 2024-08-15 15:27:00

您可以使用 LayoutInflater.from(context) 来获取您的 LayoutInflater。像这样:

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

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);
意中人 2024-08-15 15:27:00

您的编译错误是因为 BroadcastReceiver 不是从 Context 继承的。使用传递给 onReceive()Context (并摆脱 getApplicationContext() - 只需使用 Context代码>你通过了)。

现在,这可能仍然不起作用,因为我不确定您是否可以首先从 BroadcastReceiver 引发 Toast,但是这个至少会让你克服编译错误。

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.

留一抹残留的笑 2024-08-15 15:27:00

可以从活动或服务(而不是广播接收器)创建和显示 toast

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文