在我的 SMS 应用程序中,我使用SmsManager
. 之后,我想显示一个 Toast 说法"Message sent"
或"Message not sent"
. 如何检查消息是否实际发送?可能是没有连接的问题?还是没有 SIM 卡?我怎么能检测到这些?
问问题
9128 次
2 回答
12
您可以为交付和发送设置侦听器,我的方法是
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);
于 2013-09-12T18:11:30.697 回答
-5
试试下面的片段。这里的异常表示所有可能的失败情况
片段
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();
}
在这里查看一个示例
于 2013-09-12T18:11:08.670 回答