【问题标题】:Using PendingIntent to update the status of an fail sent message使用 PendingIntent 更新失败发送消息的状态
【发布时间】:2015-01-22 14:11:37
【问题描述】:

用户发送短信后,如果由于任何原因未发送,将更新STATUS column

注意:我们正在使用提供程序“content://sms/”来访问 SMS 存储库。

短信发送者类

**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不同)

【问题讨论】:

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


    【解决方案1】:

    您将额外的内容添加到sentIntent 为时已晚。您需要在通过调用PendingIntent.getBroadcast() 生成PendingIntent之前执行此操作。

    【讨论】:

      猜你喜欢
      • 2021-05-09
      • 1970-01-01
      • 1970-01-01
      • 2017-10-28
      • 1970-01-01
      • 1970-01-01
      • 2017-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多