Search code examples
androidandroid-pendingintentsmsmanager

How i can get requestcode from pending intent for showing which message delivered


I have pending intent to show message status after send

private void sendSMS(String phoneNumber, String message)
{
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}

This is my pending intent

deliveredPI = PendingIntent.getBroadcast(this, id, new Intent(DELIVERED), 0);   

When sms is delivered, I want to get requestcode from PendingIntent to show which sms is delivered, but in onReceive() method I can't get requestcode


Solution

  • You should add your SMS id to the PendingIntent like this:

    Intent deliveryIntent = new Intent(DELIVERED);
    deliveryIntent.putExtra("id", id);
    deliveredPI = PendingIntent.getBroadcast(this, id, deliveryIntent, 0);
    

    Then, in onReceive() you can get the SMS id like this:

    int id = intent.getIntExtra("id", -1);
    if (id >= 0) {
        // Got a valid id...
    }