使用PendingIntent更新失败发送消息的状态

时间:2015-01-22 14:11:38

标签: android android-intent sms android-contentprovider android-pendingintent

用户发送短信后,如果因任何原因未发送短信,则会更新STATUS column

注意:我们正在使用提供商" content:// sms /"访问SMS存储库。

SMS Sender类

**Constructor**
public SmsMessageSender(MessageRepository messageRepository, ThreadRepository threadRepository, Context context)
{
    this(messageRepository, threadRepository);
    this.sentIntent = new Intent(SmsMessageSender.SENT);
    this.sentPI = PendingIntent.getBroadcast(context, 0, this.sentIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}

@Override
public void sendMessage(String recipient, String content) {

    long threadId = threadRepository.getOrCreateThreadForRecipients(recipient);
    ArrayList<String> messageParts = generateMessages(content);

    sentIntent.putExtra("threadId", threadId);


    smsManager.sendMultipartTextMessage(recipient, null, messageParts,
            new ArrayList<>(Arrays.asList(sentPI)), null);

    Uri uri = messageRepository.insert(new Message(0, recipient, content, new Date(), null, 
        MessageStatus.Sent, threadId, Telephony.TextBasedSmsColumns.STATUS_NONE));
    sentIntent.putExtra("messageUri", uri.toString());
}

活动

@Override
public void onResume() {
    super.onResume();

    smsSentReceiver = new BroadcastReceiver(){
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            if (getResultCode() != Activity.RESULT_OK) {
                long threadId = arg1.getLongExtra("threadId", -1);
                // TODO: Update SMS message with status fail
            } else {
                // TODO: Update SMS message with status success
            }
        }
    };

    registerReceiver(smsSentReceiver, new IntentFilter(SmsMessageSender.SENT));
}

在我的活动的onReceive方法中的BroadcastReceiver上,long threadId = arg1.getLongExtra("threadId", -1);始终返回-1。 (Extra中的值与-1不同)

1 个答案:

答案 0 :(得分:0)

您要将额外费用添加到sentIntent太晚了。您需要在生成PendingIntent 之前通过调用PendingIntent.getBroadcast()来执行此操作