1

我有一个在发送短信时调用的接收器。当这种情况发生时,我想举杯。我知道向接收者展示敬酒不是一个好习惯,但我想知道为什么我的代码不起作用

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 会正确显示。

先感谢您。

4

3 回答 3

2

尝试这个

new Handler(Looper.getMainLooper()).post(new Runnable()
{
    @Override
    public void run()
    {
         Toast.makeText(context, R.string.sent, Toast.LENGTH_SHORT).show();
    }
});
于 2014-04-07T12:33:19.710 回答
0

尝试关注

  public class SmsSentReceiver extends BroadcastReceiver
 {
  @Override
  public void onReceive(Context context, Intent intent)
  {
    if (getResultCode() == Activity.RESULT_OK)
      Toast.makeText(getApplicationContext(), R.string.sent, Toast.LENGTH_SHORT).show();
    else
      Toast.makeText(getApplicationContext(), R.string.error_sending_sms, Toast.LENGTH_LONG).show();
    context.unregisterReceiver(this);
  }

}

希望这可以帮助你...

于 2014-04-07T12:31:59.800 回答
0

尝试这个:

Context c;
...
...
....

Toast.makeText(c,"Succesfully Written",Toast.LENGTH_SHORT).show();
于 2017-06-06T10:34:33.507 回答