我正在开发一个可以发送短信的项目。我已经使用两个模拟器测试了应用程序,方法是从一个发送短信并从其他模拟器接收它。
但是,如果没有成功发送短信,我想要显示吐司。但我不能在模拟器中测试它。如果我发送短信到一个不是我的接收器模拟器号码的随机数,短信仍然成功发送。所以我总是看到消息"短信被发送"。
这是我的广播接收者代码:
sentReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
Utils.print("in sentreceiver onreceive");
Utils.print(getResultCode()+"");
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
};
deliveredReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
Utils.print("in deliverreceiver onreceive");
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
};
// Receiver for Sent SMS.
registerReceiver(sentReceiver, new IntentFilter(Constants.ACTION_SMS_SENT));
// Receiver for Delivered SMS.
registerReceiver(deliveredReceiver, new IntentFilter(Constants.ACTION_SMS_DELIVERED));