BroadCastReceiver for Multiple sent Messages?

2019-04-15 17:36发布

In my app i am sending multiple sms at one time which working perfectly fine. But when i am trying to receive confirmation messages in Broadcast Receiver i am expecting to get all numbers but i end up in receiving same one number instead of n numbers.

To send SMS

Intent intent = new Intent(SENT);
intent.putExtra("phonenumber", sms_phonenumber);
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
SmsManager sms_man = SmsManager.getDefault();
sms_man.sendTextMessage(sms_phonenumber, null, sms_message, sentPI, null);

To Receive Sent Confirmation

private class SMSReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context arg0, Intent intent) {

        switch (getResultCode()) {
        case Activity.RESULT_OK: 
            String phoneNumber = intent.getExtras()
                    .getString("phonenumber");

            alertMessage("Request sent to " + phoneNumber + ".");

            break;

        case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
            String phoneNumber1 = intent.getExtras().getString(
                    "phonenumber");

            alertMessage("Sending Failed to " + phoneNumber1 + ".");

            break;

        case SmsManager.RESULT_ERROR_NO_SERVICE:
            String phone = intent.getExtras().getString(
                    "phonenumber");

            alertMessage("No service");

            break;

        case SmsManager.RESULT_ERROR_NULL_PDU:
            alertMessage("Null PDU");
            Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_SHORT)
                    .show();
            break;

        case SmsManager.RESULT_ERROR_RADIO_OFF:
            alertMessage("Radio off");
            break;
        }
    }
}

I am searched a lot in Google and SO but none of them solves my issue.

Thanks for your help.

2条回答
Ridiculous、
2楼-- · 2019-04-15 18:08

Try to send messages one after the other. Send one message...then from the on receive method, send the next message.

查看更多
萌系小妹纸
3楼-- · 2019-04-15 18:16

Try to Pending intent with all time with Unique id for Set Pending intent

PendingIntent pendingintent = PendingIntent.getService(this, uId, mIntent, PendingIntent.FLAG_ONE_SHOT);

查看更多
登录 后发表回答