Android中SMS的接收处理


在解析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]
  1. mReceiver = newRILReceiver();  
  2. mReceiverThread =new Thread(mReceiver, "RILReceiver");  
  3. mReceiverThread.start();  

其中的类型

  • mReceiver: RILReceiver
  • mReceiverThread: Thread

 

RILReceiver实现了Runnable

RILReceiver

关注RILReceiver线程的实现[在RILReceiver::run()中]

[java]
  1. public void run() {  
  2.     int retryCount= 0;  
  3.   
  4.     try {for (;;) {  
  5.         LocalSockets = null;  
  6.         LocalSocketAddress l;  
  7.   
  8.         try {  
  9.             s = newLocalSocket();  
  10.             l = newLocalSocketAddress(SOCKET_NAME_RIL,  
  11.                    LocalSocketAddress.Namespace.RESERVED);  
  12.             s.connect(l);  
  13.         } catch (IOException ex){  
  14.             // 。。。   
  15.         }  
  16.   
  17.         retryCount= 0;  
  18.         mSocket =s;  
  19.         int length= 0;  
  20.   
  21.         try {  
  22.             InputStreamis = mSocket.getInputStream();  
  23.   
  24.             for(;;) {  
  25.                Parcel p;  
  26.   
  27.                length = readRilMessage(is, buffer);  
  28.                 if(length < 0) {  
  29.                    // End-of-stream reached   
  30.                    break;  
  31.                 }  
  32.   
  33.                 p =Parcel.obtain();  
  34.                p.unmarshall(buffer, 0, length);  
  35.                p.setDataPosition(0);  
  36.   
  37.                 processResponse(p);  
  38.                p.recycle();  
  39.             }  
  40.         } catch(java.io.IOException ex) {  
  41.             // …   
  42.         } catch(Throwable tr) {  
  43.             // …   
  44.         }  
  45.   
  46.         // …   
  47.     }} catch(Throwable tr) {  
  48.        Log.e(LOG_TAG,"Uncaught exception", tr);  
  49.     }  
  50. }  

RILReceiver线程不停的监听本地Socket,读到数据之后在processResponse()[Line#37]中处理。

[java]
  1. private void processResponse (Parcel p) {  
  2.     int type;  
  3.   
  4.     type = p.readInt();  
  5.   
  6.      if(type == RESPONSE_UNSOLICITED) {  
  7.         processUnsolicited (p);  
  8.      }else if (type == RESPONSE_SOLICITED) {  
  9.         processSolicited (p);  
  10.      }  
  11.   
  12.     releaseWakeLockIfDone();  
  13.  }  

如果类型属于Unsolicited消息,则在processUnsolicited()中处理。收到的短信是属于Unsolicited信息,看它的实现。

processUnsolicited()中很长的switch… case语句中对收到短信的处理在case RIL_UNSOL_RESPONSE_NEW_SMS:

[java]
  1. SmsMessage sms;  
  2.   
  3.  sms = SmsMessage.newFromCMT(a);  
  4.  if (mSMSRegistrant != null) {  
  5.      mSMSRegistrant.notifyRegistrant(new AsyncResult(null, sms, null));  
  6.  }  
  • 1
  • 2
  • 3
  • 下一页

相关内容