【发布时间】:2014-05-19 16:44:02
【问题描述】:
我有一个在发送短信时调用的接收器。 当这种情况发生时,我想举杯。我知道向接收者展示敬酒不是一个好习惯,但我想知道为什么我的代码不起作用
public class SmsSentReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
if (getResultCode() == Activity.RESULT_OK)
Toast.makeText(context, R.string.sent, Toast.LENGTH_SHORT).show();
else
Toast.makeText(context, R.string.error_sending_sms, Toast.LENGTH_LONG).show();
context.unregisterReceiver(this);
}
}
接收者正确接收到事件,但没有显示任何 toast。 相反,如果我在调试模式下运行应用程序,其中一个 toast 会正确显示。
提前谢谢你。
【问题讨论】:
-
你怎么知道它叫什么?
-
因为我放了一个断点:)
-
如果您使用上下文,那么它将仅显示给活动。但是广播接收器可以用于多个活动。
-
@Shriram 好的,但即使使用 context.getApplicationContext() 它也不起作用
-
尝试 getBaseContext() 而不是 getApplicationContext()
标签: android broadcastreceiver android-toast