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层以后再分析,如果文中有不对的地方,请大家告诉我,谢谢!

Android 2.3发短信详细流程,布布扣,bubuko.com

时间: 07-23

Android 2.3发短信详细流程的相关文章

【转】如何向Android模拟器打电话发短信

转载地址:http://hi.baidu.com/jeremylai/item/420f9c9fe4881fccb62531f7 1. 启动Android Emulator, 查看标题栏找出端口.一般是android emulator(5554),其中5554就是端口. 2. 打开命令行,输入telnet localhost 5554.程序将会连接到android emulator,控制台会返回 Android Console: type ‘help’ for a list of command

向android模拟器打电话发短信的简单方法

在开发android应用程序时,有时候需要测试一下向android手机拨打电话发送短信时该应用程序的反应.譬如编写一个广播接收器,来提示用户有短信收到或者处理短信,就需要向该手机发送短信来进行测试.这里介绍一种简单的向android模拟器打电话发短信的方法. 该方法利用了eclipse ADT的DDMS来实现,首先点击打开DDMS,在eclipse界面的右上角,如图: 如果找不到,就点左边的图标,再点击others就会看到. 打开之后,在界面的左边中部会看见有一个Emulator Control

Android 打电话,发短信,调用系统浏览器

打电话: Intent intent = new Intent(Intent.ACTION_DIAL); intent.setData(Uri.parse("tel:10086")); startActivity(intent); 发短信: SmsManager smsManager=SmsManager.getDefault();//取得短信管理器 /**发简单的文本信息 * destinationAddress: 目标地址,发给谁 * scAddress: 来源的地址,谁发的 *

android发短信的代码

android发短信的代码 by 伍雪颖 Uri uri = Uri.parse("smsto:10086"); Intent intent = new Intent(Intent.ACTION_SENDTO,uri); intent.putExtra("sms_body","The SMS text"); startActivity(intent);

html页面通过特殊链接:打电话,发短信,发邮件详细教程

采用url href链接的方式,实现在Safari  ios,Android 浏览器,webos浏览器,塞班浏览器,IE,Operamini等主流浏览器,进行拨打电话功能. 1. 拨打电话 在电话号码前面可以加上 + (加号)表示国际号码.如: 最常用WEB页面JS实现一键拨号的电话拨打功能 例子: <a href="tel:10086">10086</a> 使用wtai协议进行拨打电话 <a href="wtai://wp/mc;10086&q

Android实例-打电话、发短信和邮件,取得手机IMEI号(XE8+小米2)

结语: 1.不提示发短信卡住,点击没有反映,我猜想,可能是因为我用的是小米手机吧. 2.接收短信报错,我猜想可能是我改了里面的方法吧(哪位大神了解,求指教). 3.project -->options…--> users permitions. 3.1 Send SMS 3.2 Read Phone State权限:将其变成 true即可. 如果 read phone state为 false,程序将启动不了,一直黑屏(我没试,原作者是这么说的,你如果没事可以试一试,试过记得给我说一声哦).

Android:调用打电话和发短信及intent一些其他用法

1.打电话 可以自己写界面,在button的单击事件中添加如下代码即可: Intent intent = new Intent();    intent.setAction("android.intent.action.CALL");    intent.setData(Uri.parse("tel:"+ mobile));//mobile为你要拨打的电话号码,模拟器中为模拟器编号也可    startActivity(intent); 需要添加打电话权限: <

Android小功能实现-sim卡读取,发短信,播放音乐

1.读取sim卡信息 private TelephonyManager tm; tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); String sim = tm.getSimSerialNumber(); 需要TelephonyManager 加权限 <uses-permission android:name="android.permission.READ_PHONE_STATE"/> 2.发短信 S

Android开发,URI 如:发短信,发彩信,调用通讯录等

一.直接拨打电话,与三不同的是,这个直接拨打电话,而不是打开拨号界面 Uri uri = Uri.parse("tel:10086"); Intent intent = new Intent(Intent.ACTION_CALL, uri); 二.打开拨号界面,类型是Intent.ACTION_DIAL Uri uri = Uri.parse("tel:10086"); Intent intent = new Intent(Intent.ACTION_DIAL, uri); 三.打开一个网页,类