在解析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: Thread
RILReceiver實現了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));
- }