在我的短信应用程序中,我使用SmsManager
发送短信。之后,我想要显示一个说"Message sent"
或"Message not sent"
的Toast。我如何检查邮件是否实际发送?可能就像一个没有连接的问题?还是没有SIM卡?我怎么能发现这些?
答案 0 :(得分:10)
你可以获得传送和发送我的方法的听众
PendingIntent sentPI = PendingIntent.getBroadcast(mContext, 0, new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(mContext, 0,new Intent(DELIVERED), 0);
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
// ---when the SMS has been sent---
mContext.registerReceiver(
new BroadcastReceiver()
{
@Override
public void onReceive(Context arg0,Intent arg1)
{
switch(getResultCode())
{
case Activity.RESULT_OK:
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
break;
}
}
}, new IntentFilter(SENT));
// ---when the SMS has been delivered---
mContext.registerReceiver(
new BroadcastReceiver()
{
@Override
public void onReceive(Context arg0,Intent arg1)
{
switch(getResultCode())
{
case Activity.RESULT_OK:
break;
case Activity.RESULT_CANCELED:
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null,message,sentPI, deliveredPI);
答案 1 :(得分:-3)
尝试下面的代码段。此异常表示所有可能的故障情况
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(number, null, sms, null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "SMS failed, please try again later!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
查看示例here