如何从待处理的意图中获取请求代码以显示传递的消息

时间:2016-02-27 20:47:43

标签: android android-pendingintent smsmanager

我有意图在发送

后显示消息状态
private void sendSMS(String phoneNumber, String message)
{
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}

这是我未决的意图

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

当短信发送时,我想从requestcode获取PendingIntent以显示已发送的短信,但在onReceive()方法中我无法获取requestcode

1 个答案:

答案 0 :(得分:0)

您应将短信id添加到PendingIntent,如下所示:

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

然后,在onReceive()中,您可以获得这样的短信ID:

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