当前位置:操作系统 > 安卓/Android >>

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]
mReceiver = newRILReceiver(); 
mReceiverThread =new Thread(mReceiver, "RILReceiver"); 
mReceiverThread.start(); 

其中的类型
mReceiver: RILReceiver
mReceiverThread: Thread
 
RILReceiver实现了Runnable
RILReceiver

 

关注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); 
    } 

        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
                     

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,