我有一种发送短信的方法。此方法注册两个待处理的意图来接收发送和传递报告。
我发送第一条消息并收到它的发送和传递报告,然后发送另一条消息。在接收第二条消息的报告时,我收到第一条消息的报告,然后是第二条消息。如果我发送另一条消息,我会收到第一条和第二条消息的报告,然后是第三条消息。
有什么问题?
来源:
public void sendSms(String phoneNumber, String cmd ...) {
sentPI = PendingIntent.getBroadcast(this, sendSmsCounter++, new Intent(SENT), PendingIntent.FLAG_CANCEL_CURRENT);
deliveredPI = PendingIntent.getBroadcast(this, deliverCounter++, new Intent(DELIVERED), PendingIntent.FLAG_CANCEL_CURRENT);
sendingBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode())
{
case Activity.RESULT_OK:
displayMessage("Sms Sent");
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
displayMessage("Sending SMS error: Generic failure");
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
displayMessage("Sending SMS error: No service");
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
displayMessage("Sending SMS error: Null PDU");
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
displayMessage("Sending SMS error: Radio off");
break;
}
}
};
this.registerReceiver(sendingBroadcastReceiver, new IntentFilter(SENT));
deliveryBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode())
{
case Activity.RESULT_OK:
displayMessage("SMS Delivered");
break;
case Activity.RESULT_CANCELED:
displayMessage("SMS delivery error: SMS Canceled");
break;
}
}
};
this.registerReceiver(deliveryBroadcastReceiver, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, cmd, sentPI, deliveredPI);