在解析WAPPUSH over SMS时,看了一下Android里SMS接收的流程,并按照自己需要的流程记录,其他的分支处理并未讲述。PDU数据的encode/decode并未解析,有兴趣的读者可以到相应的代码处自己解读一下。
Android中,RIL用RILReciever接收SMS pdu,并根据不同的信息类型用相应函数来处理。因手机制式的差异,用GsmSmsDispatcher或CdmaSmsDispatcher来做各自的消息处理并分发。最后的分发是通过发送相应的Broadcast,所以,对感兴趣的消息处理,可以注册Receiver来监听相应的Broadcast,实现自己的SMS/MMS/Wap push,以及其他类型消息的接收处理。
RIL构造函数中,Receiver的初始化[在文件RIL.java中]
[java]
mReceiver = newRILReceiver(); mReceiverThread =new Thread(mReceiver, "RILReceiver"); mReceiverThread.start();其中的类型
mReceiver: RILReceiver mReceiverThread: ThreadRILReceiver实现了Runnable
关注RILReceiver线程的实现[在RILReceiver::run()中]
[java]
public void run() { int retryCount= 0; try {for (;;) { LocalSockets = null; LocalSocketAddress l; try { s = newLocalSocket(); l = newLocalSocketAddress(SOCKET_NAME_RIL, LocalSocketAddress.Namespace.RESERVED); s.connect(l); } catch (IOException ex){ // 。。。 } retryCount= 0; mSocket =s; int length= 0; try { InputStreamis = mSocket.getInputStream(); for(;;) { Parcel p; length = readRilMessage(is, buffer); if(length < 0) { // End-of-stream reached break; } p =Parcel.obtain(); p.unmarshall(buffer, 0, length); p.setDataPosition(0); processResponse(p); p.recycle(); } } catch(java.io.IOException ex) { // … } catch(Throwable tr) { // … } // … }} catch(Throwable tr) { Log.e(LOG_TAG,"Uncaught exception", tr); } }RILReceiver线程不停的监听本地Socket,读到数据之后在processResponse()[Line#37]中处理。
[java]
private void processResponse (Parcel p) { int type; type = p.readInt(); if(type == RESPONSE_UNSOLICITED) { processUnsolicited (p); }else if (type == RESPONSE_SOLICITED) { processSolicited (p); } releaseWakeLockIfDone(); }如果类型属于Unsolicited消息,则在processUnsolicited()中处理。收到的短信是属于Unsolicited信息,看它的实现。
processUnsolicited()中很长的switch… case语句中对收到短信的处理在case RIL_UNSOL_RESPONSE_NEW_SMS:
[java]
SmsMessage sms; sms = SmsMessage.newFromCMT(a); if (mSMSRegistrant != null) { mSMSRegistrant.notifyRegistrant(new AsyncResult(null, sms, null)); }