zoukankan      html  css  js  c++  java
  • Android 2.3发短信详细流程

    在android中,APP通过SmsManager.java一系列方法实现发送短信的功能,而发送的内容有很很多种,比如sendTextMessage、sendMultipartTextMessage、sendDataMessage等等,在这篇文章里我们就以其中一个为例阐述发送短信的完整流程,如果有不对的地方,请大家指正,一起学习。

    1. 起点:SmsManager.java (frameworks/base/telephony/java/android/telephony/SmsManager.java)

    sendTextMessage的核心代码如下:


    view plaincopy to clipboardprint?public void sendTextMessage( 
              ...... 
           try { 
               ISms iccISms = ISms.Stub.asInterface(ServiceManager.getService("isms")); 
               if (iccISms != null) { 
                   iccISms.sendText(destinationAddress, scAddress, text, sentIntent, deliveryIntent); 
               } 
           } catch (RemoteException ex) { 
               // ignore it  
           } 
       } 
     public void sendTextMessage(
               ......
            try {
                ISms iccISms = ISms.Stub.asInterface(ServiceManager.getService("isms"));
                if (iccISms != null) {
                    iccISms.sendText(destinationAddress, scAddress, text, sentIntent, deliveryIntent);
                }
            } catch (RemoteException ex) {
                // ignore it
            }
        }其中,view plaincopy to clipboardprint?ISms iccISms = ISms.Stub.asInterface(ServiceManager.getService("isms")); 
    ISms iccISms = ISms.Stub.asInterface(ServiceManager.getService("isms"));是通过AIDL的方式,获得服务,再调用这个服务对象的sendText()方法,那这个服务对象在哪里呢?
    2. 我们知道,在eclipse中创建一个xx.aidl文件后,IDE会利用相关工具自动生成一个名为xx.java的接口,它有一个名为Stub的内部类,那我们自己创建一个类并继承这个内部类,则可以实现了进程间的通信,这个是aidl的知识,这儿不详述。我们往下看:

    根据aidl的实现流程,那该服务对象应该是继承了ISms.Stub,经过查找我们发现这个服务类:IccSmsInterfaceManagerProxy.java,所以从SmsManager.sendTextMessage()方法调用了IccSmsInterfaceManagerProxy对象的sendText()方法。

    3. 第二阶段:IccSmsInterfaceManagerProxy.java(frameworks/base/telephony/java/com/android/internal/telephony/IccSmsInterfaceManagerProxy)

    我们看IccSmsInterfaceManagerProxy的sendText()方法核心代码:

    view plaincopy to clipboardprint?private IccSmsInterfaceManager mIccSmsInterfaceManager; 
    ...... 
    public void sendText(String destAddr, String scAddr, 
                String text, PendingIntent sentIntent, PendingIntent deliveryIntent) { 
            mIccSmsInterfaceManager.sendText(destAddr, scAddr, text, sentIntent, deliveryIntent); 
        } 
    private IccSmsInterfaceManager mIccSmsInterfaceManager;
    ......
    public void sendText(String destAddr, String scAddr,
                String text, PendingIntent sentIntent, PendingIntent deliveryIntent) {
            mIccSmsInterfaceManager.sendText(destAddr, scAddr, text, sentIntent, deliveryIntent);
        }
    继续调用,此时调用的是IccSmsInterfaceManager对象的sendText()方法,那IccSmsInterfaceManager是什么玩意??

    4. 第三阶段:IccSmsInterfaceManager.java(frameworks/base/telephony/java/com/android/internal/telephony/IccSmsInterfaceManager.java)

    从代码看出IccSmsInterfaceManager是一个继承了ISms.Stub的抽象类,相关核心代码如下:

    view plaincopy to clipboardprint?protected SMSDispatcher mDispatcher; 
    public void sendText(String destAddr, String scAddr, 
                String text, PendingIntent sentIntent, PendingIntent deliveryIntent) { 
            mPhone.getContext().enforceCallingPermission( 
                    "android.permission.SEND_SMS", 
                    "Sending SMS message"); 
            if (Log.isLoggable("SMS", Log.VERBOSE)) { 
                log("sendText: destAddr=" + destAddr + " scAddr=" + scAddr + 
                    " text='"+ text + "' sentIntent=" + 
                    sentIntent + " deliveryIntent=" + deliveryIntent); 
            } 
            mDispatcher.sendText(destAddr, scAddr, text, sentIntent, deliveryIntent); 
        } 
    protected SMSDispatcher mDispatcher;
    public void sendText(String destAddr, String scAddr,
                String text, PendingIntent sentIntent, PendingIntent deliveryIntent) {
            mPhone.getContext().enforceCallingPermission(
                    "android.permission.SEND_SMS",
                    "Sending SMS message");
            if (Log.isLoggable("SMS", Log.VERBOSE)) {
                log("sendText: destAddr=" + destAddr + " scAddr=" + scAddr +
                    " text='"+ text + "' sentIntent=" +
                    sentIntent + " deliveryIntent=" + deliveryIntent);
            }
            mDispatcher.sendText(destAddr, scAddr, text, sentIntent, deliveryIntent);
        }
    IccSmsInterfaceManager对象的sendText()方法调用了SMSDispatcher类的sendText()方法,继续往下:
    5. 第四阶段:SMSDispatcher.java(frameworks/base/telephony/java/com/android/internal/telephony/SMSDispatcher.java)

    该类是一个抽象类,它的sendText()并没有实现,它的实现类是GsmSMSDispatcher.java或者CdmaSMSDispatcher.java,假设我们用的GSM网络,则此时调用到GsmSMSDispatcher的sendText()方法。

    6. 第五阶段:GsmSMSDispatcher.java(frameworks/base/telephony/java/com/android/internal/telephony/gsm/GsmSMSDispatcher.java)

    核心代码如下:

    view plaincopy to clipboardprint?protected void sendText(String destAddr, String scAddr, String text, 
                PendingIntent sentIntent, PendingIntent deliveryIntent) { 
            SmsMessage.SubmitPdu pdu = SmsMessage.getSubmitPdu( 
                    scAddr, destAddr, text, (deliveryIntent != null)); 
            sendRawPdu(pdu.encodedScAddress, pdu.encodedMessage, sentIntent, deliveryIntent); 
        } 
    ...... 
    protected void sendText(String destAddr, String scAddr, String text,
                PendingIntent sentIntent, PendingIntent deliveryIntent) {
            SmsMessage.SubmitPdu pdu = SmsMessage.getSubmitPdu(
                    scAddr, destAddr, text, (deliveryIntent != null));
            sendRawPdu(pdu.encodedScAddress, pdu.encodedMessage, sentIntent, deliveryIntent);
        }
    ......

    到这儿,"sendText()"这个字眼就没了,换成了另外一个方法名:sendRawPdu(),追踪这个方法可以发现它是SMSDispatcher.java的一个方法,这个类看着很眼熟吧?不错,在第四阶段我们已经和它打过交道了!我们来看看它的sendRawPdu到底是干嘛的:view plaincopy to clipboardprint?protected void sendRawPdu(byte[] smsc, byte[] pdu, PendingIntent sentIntent, 
                PendingIntent deliveryIntent) { 
               ...... 
               sendSms(tracker); 
               ..... 
     } 
    protected void sendRawPdu(byte[] smsc, byte[] pdu, PendingIntent sentIntent,
                PendingIntent deliveryIntent) {
               ......
               sendSms(tracker);
               .....
     }又来了一个新方法名:sendSms(),从sendRawPdu()传来的信息经过封装传递给sendSms()方法进行处理,而在SMSDispatcher.java中,这个方法只是声明了一下,它的具体实现由子类:GsmSMSDispatcher.java完成。下面我们来看GsmSMSDispatcher.java
    7. 第六阶段:GsmSMSDispatcher.java(frameworks/base/telephony/java/com/android/internal/telephony/gsm/GsmSMSDispatcher.java)

    GsmSMSDispatcher.java的sendSms()方法核心代码如下:

    view plaincopy to clipboardprint?protected CommandsInterface mCm; 
     
    protected void sendSms(SmsTracker tracker) { 
            HashMap map = tracker.mData; 
     
            byte smsc[] = (byte[]) map.get("smsc"); 
            byte pdu[] = (byte[]) map.get("pdu"); 
     
            Message reply = obtainMessage(EVENT_SEND_SMS_COMPLETE, tracker); 
            mCm.sendSMS(IccUtils.bytesToHexString(smsc), 
                    IccUtils.bytesToHexString(pdu), reply); 
        } 
    protected CommandsInterface mCm;

    protected void sendSms(SmsTracker tracker) {
            HashMap map = tracker.mData;

            byte smsc[] = (byte[]) map.get("smsc");
            byte pdu[] = (byte[]) map.get("pdu");

            Message reply = obtainMessage(EVENT_SEND_SMS_COMPLETE, tracker);
            mCm.sendSMS(IccUtils.bytesToHexString(smsc),
                    IccUtils.bytesToHexString(pdu), reply);
        }离成功已经不远了....
    我们知道,CommandsInterface是一个特殊的接口,它的RIL.java息息相关,而在上面的代码中sendSms()调用来CommandsInterface对象的sendSMS()方法来做事情,而CommandsIterface是一个接口,所以事情只好由它的儿子(其实是孙子,RIL的爸爸BaseCommands是CommandsInterface的儿子)来完成,好,进入RIL.java.

    8. 第七阶段:RIL.java(/frameworks/base/telephony/java/com/android/internal/telephony/RIL.java)

    只要研究过ril层的,对这玩意都一定很熟悉,所以直接看它的sendSMS()方法:

    view plaincopy to clipboardprint?public void sendSMS (String smscPDU, String pdu, Message result) { 
            RILRequest rr 
                    = RILRequest.obtain(RIL_REQUEST_SEND_SMS, result); 
     
            rr.mp.writeInt(2); 
            rr.mp.writeString(smscPDU); 
            rr.mp.writeString(pdu); 
     
            if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest)); 
     
            send(rr); 
        } 
     
    //send(RILRequest rr)  
    <pre name="code" class="java">private void 
        send(RILRequest rr) { 
            Message msg; 
     
            msg = mSender.obtainMessage(EVENT_SEND, rr); 
     
            acquireWakeLock(); 
     
            msg.sendToTarget(); 
        } 
    public void sendSMS (String smscPDU, String pdu, Message result) {
            RILRequest rr
                    = RILRequest.obtain(RIL_REQUEST_SEND_SMS, result);

            rr.mp.writeInt(2);
            rr.mp.writeString(smscPDU);
            rr.mp.writeString(pdu);

            if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));

            send(rr);
        }

    //send(RILRequest rr)
    <pre name="code" class="java">private void
        send(RILRequest rr) {
            Message msg;

            msg = mSender.obtainMessage(EVENT_SEND, rr);

            acquireWakeLock();

            msg.sendToTarget();
        }

    OK!在sendSMS()方法中,我们把上面所传下来的东东写入到Parcel中,协同一个特殊的RILRequest被发送出去,发送到哪里了?接着看:view plaincopy to clipboardprint?public void 
            handleMessage(Message msg) { 
                RILRequest rr = (RILRequest)(msg.obj); 
                RILRequest req = null; 
     
                switch (msg.what) { 
                    case EVENT_SEND: 
                        boolean alreadySubtracted = false; 
                        try { 
                          LocalSocket s; 
                          ...... 
                          s.getOutputStream().write(dataLength); 
                          s.getOutputStream().write(data); 
                        } catch (IOException ex) { 
                          ......      
                        } 
                        break; 
                } 
            } 
    public void
            handleMessage(Message msg) {
                RILRequest rr = (RILRequest)(msg.obj);
                RILRequest req = null;

                switch (msg.what) {
                    case EVENT_SEND:
                        boolean alreadySubtracted = false;
                        try {
                          LocalSocket s;
                          ......
                          s.getOutputStream().write(dataLength);
                          s.getOutputStream().write(data);
                        } catch (IOException ex) {
                          ......    
                        }
                        break;
                }
            }重点:LocalSocket、s.getOutputStream().write(data)
    我们把短信相关的数据及特殊RILRequst对象写入到Socket的的输出流中,进而将数据传递到RIL层,即底层,然后RIL层通过接收Socket中传过来的数据解析得到请求内容并进行处理,到此,发短信的Java部分讲完了。

    RIL层以后再分析,如果文中有不对的地方,请大家告诉我,谢谢!

  • 相关阅读:
    Qt 解析EXcel文件
    Qt PC 安卓 tcp传输文件
    Qt listwigwt item 加入自定义元素
    Qt 独立运行时伴随CMD命令窗口
    xml模块
    shelve模块
    json模块 pickle模块
    sys 模块
    os模块
    添加变量
  • 原文地址:https://www.cnblogs.com/kevincode/p/3868380.html
Copyright © 2011-2022 走看看