使用 PendingIntent 更新失败发送消息的状态
Using PendingIntent to update the status of an fail sent message
用户发送 SMS 后,如果由于任何原因未发送,将更新 STATUS column。
注意:我们正在使用提供商 "content://sms/" 访问 SMS 存储库。
短信发件人class
**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());
}
Activity
@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));
}
在我 activity 的 onReceive
方法中的 BroadcastReceiver 上,long threadId = arg1.getLongExtra("threadId", -1);
总是 returns -1。 (Extra的值和-1不一样)
您向 sentIntent
添加额外内容已经太晚了。在通过调用 PendingIntent.getBroadcast()
.
生成 PendingIntent
之前,您需要执行此操作
用户发送 SMS 后,如果由于任何原因未发送,将更新 STATUS column。
注意:我们正在使用提供商 "content://sms/" 访问 SMS 存储库。
短信发件人class
**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());
}
Activity
@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));
}
在我 activity 的 onReceive
方法中的 BroadcastReceiver 上,long threadId = arg1.getLongExtra("threadId", -1);
总是 returns -1。 (Extra的值和-1不一样)
您向 sentIntent
添加额外内容已经太晚了。在通过调用 PendingIntent.getBroadcast()
.
PendingIntent
之前,您需要执行此操作