Android 短信发送失败是如何监听到的

之前做项目,遇到需求:在短信发送失败后,弹出对话框,提示失败,如果用户点击retry,则开始重新发送,否则什么都不干。

高通8.0短信发送失败处理
平台发送短信后会监听短信是否发送成功,这里主要看失败的情况
如果失败有两个处理,两个处理分隔开(即没有直接关系)
1.发送短信失败的通知
2.更改UI,将短信sending状态改为failed状态,允许resend
也就是说,短信失败的话有两个地方可以知道,1是通知栏会有提示,2是短信item项状态会变成失败状态,此时点击item会重新发送

注意目前只是作简单的记录,由于当时任务时间紧迫,没有仔细研究

通知的实现

点击发送后流程如下:
handler发送消息
SmsSingleRecipientSender.sendMessage
SmsSingleRecipientSender intent.addAction MESSAGE_SENT_ACTION
sentIntents.add(PendingIntent.getBroadcast(mContext, requestCode, intent, 0));

handler处理消息
SmsReceiverService.handleMessage(case Action MESSAGE_SENT_ACTION)
SmsReceiverService.handleSmsSent
SmsReceiverService.messageFailedToSend
MessagingNotification.notifySendFailed
MessagingNotification.notificationMgr.notify(MESSAGE_FAILED_NOTIFICATION_ID, notification);
这里应该需要关注PendingIntent,应该就是这个PendingIntent来实时发现短信的发送失败的。
当时的需求可以在弹出通知同时显示短信发送失败的对话框。

界面UI更改的实现

应该是:监听数据库变化,更改界面

MessageListAdapter:CursorAdapter.changeCursor
MessageListAdapter:BaseAdapter.notifyDataSetChanged
MessageListAdapter:CursorAdapter.bindView
MessageListItem:bind
MessageListItem:drawRightStatusIndicator

此处应该关心的应该是ListAdapter如何监听数据库变化。

retry实现

MessageListItem.onMessageListItemClick
MessageListItem.sendMessage(mMessageItem, MSG_LIST_RESEND);
retry就是点击按钮时查看短信的状态,如果是失败的,就用handler发送消息,实现重新发送

总结

所以短信发送失败的状态如何被监听到呢,有2条路
1,使用listAdapter监听数据库变化
2,使用pendingIntent监听发送状态

猜你喜欢

转载自blog.csdn.net/u011109881/article/details/80461848