发彩信和发短信一样,在ComposeMessageActivity.java界面都是从onclick()、sendMessage()开始,同样的发送前检查收件人是否有效,和短信不一样的是发彩信的时候邮件地址也是可以做为收件人的,接下来也是WorkingMessage.java的send()方法。
if (requiresMms() || addressContainsEmailToMms(conv, msgTxt)) { // uaProfUrl setting in mms_config.xml must be present to send an MMS. // However, SMS service will still work in the absence of a uaProfUrl address. if (MmsConfig.getUaProfUrl() == null) { //UA user-agent是一定要有的,UA通常包括了手机厂商、硬件、软件、系统和浏览器等信息 //这个代码是4.0才有的,如果UA为空,可以发送成功,但接收方无法收到。 String err = "WorkingMessage.send MMS sending failure. mms_config.xml is " + "missing uaProfUrl setting. uaProfUrl is required for MMS service, " + "but can be absent for SMS."; RuntimeException ex = new ContentRestrictionException(err); Log.e(TAG, err, ex); // now, let's just crash. throw ex; }...........省略了部分代码....................... // Do the dirty work of sending the message off of the main UI thread. new Thread(new Runnable() { public void run() { final SendReq sendReq = makeSendReq(conv, subject); slideshow.prepareForSend(); sendMmsWorker(conv, mmsUri, persister, slideshow, sendReq); updateSendStats(conv); } }).start(); }上面的代码只是准备,sendMmsWorker()是接下来的发送入口,看下这个函数里面有什么吧,同样省略了不是很重要的代码。
private void sendMmsWorker(Conversation conv, Uri mmsUri, PduPersister persister, SlideshowModel slideshow, SendReq sendReq) { ...省略部分代码..................... if (mmsUri == null) { // Create a new MMS message if one hasn't been made yet. mmsUri = createDraftMmsMessage(persister, sendReq, slideshow); } else { // Otherwise, sync the MMS message in progress to disk. updateDraftMmsMessage(mmsUri, persister, slideshow, sendReq); } ...省略部分代码..................... MessageSender sender = new MmsMessageSender(mActivity, mmsUri, slideshow.getCurrentMessageSize()); try { if (!sender.sendMessage(threadId)) { // The message was sent through SMS protocol, we should // delete the copy which was previously saved in MMS drafts. SqliteWrapper.delete(mActivity, mContentResolver, mmsUri, null, null); } // Make sure this thread isn't over the limits in message count Recycler.getMmsRecycler().deleteOldMessagesByThreadId(mActivity, threadId); } catch (Exception e) { Log.e(TAG, "Failed to send message: " + mmsUri + ", threadId=" + threadId, e); }主要就是贴出来的这两部分。createDraftMmsMessage()这个方法会把slideshow对象里的幻灯片信息转化成PduPart的字节数组,也就是把彩信里的媒体文件(图片、音频、视频和其它类型的文件 )编码,具体过程可以看SlideShowModel.java类的makePduBody(),至于为什么写那些字段,就要看OMA的相关文档了,比如这个 WAP-209-MMSEncapsulation-20020105-a.pdf文档。再往下是TransactionService.java的事了。
TransactionService.java这个类我认为可以算彩信的关键代码了,这里涉及网络状态的判断,beginMmsConnectivity()/endMmsConnectivity()对应彩信网络的开启和关闭,彩信的发送、接收(自动下载和手动下载)都在这里判断并转给对应的sendTransaction.java、NotificationTransaction.java和RetriveTransaction.java的处理,把这个类里面的log全开,你可以得到很多有用的信息,试试就知道了。既然是发送流程,我们关注sendTransaction.java这个类好了, 线程发送是必须的,sendTransaction.java实现Runnable接口,那么真正的发送在run()方法里面了,看下面给出的代码,其它的都是辅助功能,有用但不是最关键的。
byte[] response = sendPdu(SendingProgressTokenManager.get(tokenKey),new PduComposer(mContext, sendReq).make());接下会跑到Transaction.java类的这个方法里 ,到这里彩信在信息应用的路算是走完了,可以看到彩信是通过http协议来传输数据的。
protected byte[] sendPdu(long token, byte[] pdu, String mmscUrl) throws IOException, MmsException { if (pdu == null) { throw new MmsException(); } ensureRouteToHost(mmscUrl, mTransactionSettings); return HttpUtils.httpConnection( mContext, token, mmscUrl, pdu, HttpUtils.HTTP_POST_METHOD, mTransactionSettings.isProxySet(), mTransactionSettings.getProxyAddress(), mTransactionSettings.getProxyPort()); }发送的流程大致就这些,当然彩信里面细节知识还有很多,这里只是个大概的流程,希望对刚接触andorid mms的同学有所帮助 ,最后给出一个短信彩信完整的发送流程图,前面短信发送那篇说好有个图,一起放这里了。