Android入门-MMS-短信/彩信发送流程+代码分析

时间:2021-12-08 08:55:18

WorkingMessage获取将要发送的短信

首先刷新收信人,然后创建SmsMessageSender类并调用sendMessage()发送短信。

private void sendSmsWorker(String msgText, String semiSepRecipients, long threadId, int simId) {
String[] dests = TextUtils.split(semiSepRecipients, ";");
MessageSender sender = new SmsMessageSender(mActivity, dests, msgText, threadId);
// 双卡支持 @{
sender.setSimId(simId);
// @}

try {
sender.sendMessage(threadId);//调用SmsMessageSender的sendMessage方法发送短信

// 确认该线程没有超过最大短信数
Recycler.getSmsRecycler().deleteOldMessagesByThreadId(mActivity, threadId);
} catch (Exception e) {
Log.e(TAG, "Failed to send SMS message, threadId=" + threadId, e);
}

mStatusListener.onMessageSent();
MmsWidgetProvider.notifyDatasetChanged(mActivity);
/// M:
SmsReceiverService.sSmsSent = false;
}
}

 

在sendMessage()方法中实际调用queueMessage(),相当于将短信加入到发送队列。看看queueMessage()的实现。

    private boolean queueMessage(long token) throws MmsException {
if ((mMessageText == null) || "".equals(mMessageText.trim()) || (mNumberOfDests == 0)) {
// 不允许发送空短信
throw new MmsException("Null message body or dest.");
}

for (int i = 0; i < mNumberOfDests; i++) {
try {//将短信添加到数据库
EncapsulatedTelephony.Sms.addMessageToUri(mContext.getContentResolver(),
Uri.parse("content://sms/queued"), mDests[i],
mMessageText, null, mTimestamp,
true /* read */,
requestDeliveryReport,
mThreadId,
mSimId);
} catch (SQLiteException e) {
SqliteWrapper.checkSQLiteException(mContext, e);
}
}
//为发送信息添加广播通知,实时反馈短信发送状态
Intent sentIt = new Intent(SmsReceiverService.ACTION_SEND_MESSAGE, null, mContext,
SmsReceiver.class);
mContext.sendBroadcast(sentIt);
return false;
}


 

彩信的发送相对复杂,WorkingMessage获取将要发送的彩信,刷新联系人,再生成彩信可发送的pdu,存入数据库。创建MmsMessageSender并存入彩信发送的uri。

private void sendMmsWorker(Conversation conv, Uri mmsUri,
PduPersister persister, SlideshowModel slideshow, SendReq sendReq,
int simId) {
long threadId = 0;
Cursor cursor = null;
boolean newMessage = false;
try {
// 首先将彩信存入数据库
DraftCache.getInstance().setSavingDraft(true);
mStatusListener.onPreMessageSent();

// 确认接收彩信人id.
threadId = conv.ensureThreadId();

// 确认接收者地址.
String[] dests = conv.getRecipients().getNumbers(true);
if (dests.length == 1) {
// 确认地址与数据库中匹配,如果不匹配需要更新SendReq.
String newAddress = Conversation.verifySingleRecipient(
mActivity, conv.getThreadId(), dests[0]);

if (!newAddress.equals(dests[0])) {
dests[0] = newAddress;
EncodedStringValue[] encodedNumbers = EncodedStringValue
.encodeStrings(dests);
if (encodedNumbers != null) {
sendReq.setTo(encodedNumbers);
}
}
}
newMessage = mmsUri == null;
if (newMessage {
// 显示彩信的正在发送状态
ContentValues values = new ContentValues();
values.put(Mms.MESSAGE_BOX, Mms.MESSAGE_BOX_OUTBOX);
values.put(Mms.THREAD_ID, threadId);
values.put(Mms.MESSAGE_TYPE, PduHeaders.MESSAGE_TYPE_SEND_REQ);
// 双卡支持. @{
values.put(EncapsulatedTelephony.Mms.SIM_ID, simId);
// / @}
mmsUri = SqliteWrapper.insert(mActivity, mContentResolver,
Mms.Outbox.CONTENT_URI, values);
}
mStatusListener.onMessageSent();

// 确认试图发送的信息.
UserHappinessSignals.userAcceptedImeText(mActivity);

// 确认不能有太多未发送信息.
cursor = SqliteWrapper.query(mActivity, mContentResolver,
Mms.Outbox.CONTENT_URI, MMS_OUTBOX_PROJECTION, null, null,
null);
if (cursor != null) {
long maxMessageSize = MmsConfig
.getMaxSizeScaleForPendingMmsAllowed() *
// / M: change google default. @{
MmsConfig.getUserSetMmsSizeLimit(true);
// / @}
long totalPendingSize = 0;
while (cursor.moveToNext()) {
// / M: change google default. @{
if (PduHeaders.STATUS_UNREACHABLE != cursor
.getLong(MMS_MESSAGE_STATUS_INDEX)) {
totalPendingSize += cursor
.getLong(MMS_MESSAGE_SIZE_INDEX);
}
// / @}
}
if (totalPendingSize >= maxMessageSize) {
unDiscard(); // 允许将发送失败彩信保存为草稿.
mStatusListener.onMaxPendingMessagesReached();
markMmsMessageWithError(mmsUri);
return;
}
}
} finally {
if (cursor != null) {
cursor.close();
}
}

try {
if (newMessage) {
// 如果还没有创建新彩信,创建一个.
mmsUri = createDraftMmsMessage(persister, sendReq, slideshow,
mmsUri, mActivity);
} else {
// 或者同步正在编辑的彩信.
updateDraftMmsMessage(mmsUri, persister, slideshow, sendReq);
}

// 删除彩信草稿.
deleteDraftSmsMessage(threadId);
} finally {
DraftCache.getInstance().setSavingDraft(false);
}

// 将已发送彩信设置为丢弃状态.
mDiscarded = true;

try {
MessageSender sender = new MmsMessageSender(mActivity, mmsUri,
slideshow.getCurrentSlideshowSize());
// 双卡支持. @{
sender.setSimId(simId);
// / @}
if (!sender.sendMessage(threadId)) {
// 信息通过sms协议发送,需要删除保存在彩信草稿中的副本.
SqliteWrapper.delete(mActivity, mContentResolver, mmsUri, null,
null);
}

// 确保未超过最大短信数
Recycler.getMmsRecycler().deleteOldMessagesByThreadId(mActivity,
threadId);
} catch (Exception e) {
Log.e(TAG, "Failed to send message: " + mmsUri + ", threadId="
+ threadId, e);
}
MmsWidgetProvider.notifyDatasetChanged(mActivity);
SendTransaction.sMMSSent = false;
}