android 短信的接收和发送处理

时间:2021-12-08 08:55:06

一 初始化

 

手机开机初始化调用GSMPhone 构造函数。

GSMPhone (Context context, CommandsInterface ci, PhoneNotifier notifier, boolean unitTestMode)

创建  mSMS = new GsmSMSDispatcher(this);

该类继承于SMSDispatcher。类SMSDispatcher中构造函数中初始化了 短信的消息

        mCm.setOnNewSMS(this, EVENT_NEW_SMS, null);

        mCm.setOnSmsStatus(this, EVENT_NEW_SMS_STATUS_REPORT, null);

        mCm.setOnIccSmsFull(this, EVENT_ICC_FULL, null);

handleMessage定义了sms的消息处理函数

public void handleMessage(Message msg) {

……

        case EVENT_NEW_SMS:

……

}

 

mSimSmsIntManager = new SimSmsInterfaceManager(this);

SimSmsInterfaceManager继承于IccSmsInterfaceManager 为Isms.stub的实现类.

在IccSmsInterfaceManager 类实现了 Isms.adil. 中定义的方法,以实现远程调用。

 

二 短信发送

 

短信发送调用接口

        SmsManager sms = SmsManager.getDefault();

       sms.sendTextMessage(string1, null, string2, p, null);

sendTextMessage 调用 sendRawPdu。

在sendRawPdu中 创建了

ISms iccISms = ISms.Stub.asInterface(ServiceManager.getService("isms"));

通过aidl接口,实例化 IccSmsInterfaceManager. 调用 smsDispatcher.Java

protected void sendRawPdu(byte[] smsc, byte[] pdu, PendingIntent sentIntent,

            PendingIntent deliveryIntent) 函数。

而后通过stub 接口调用sendSMS 接口。

RIL.java 中 sendSMS

    public void

       sendSMS (String smscPDU, String pdu, Message result) {

        RILRequest rr

                = RILRequest.obtain(RIL_REQUEST_SEND_SMS, result);

        …..

        send(rr);

    }

发送 RIL_REQUEST_SEND_SMS 的 Request 请求到rild层。

Rild与modem之间联系与其它应用类似,不再重复。

 

 

三 短信的接收

 

Modem 与rild之间不再重复。从ril层中收到消息开始。  

Ril.java中 rilReceiver 收到信短信消息processResponse(p); 收到短信属于主动上报

调用processUnsolicited函数。

    private void  processUnsolicited (Parcel p) {

………

          case RIL_UNSOL_RESPONSE_NEW_SMS: ret =  responseString(p); break;

              …….

          switch(response) {

      

            case RIL_UNSOL_RESPONSE_NEW_SMS: {

            …….

                SmsMessage sms;

            …….

                sms = SmsMessage.newFromCMT(a);

                if (mSMSRegistrant != null) {

                    mSMSRegistrant

                        .notifyRegistrant(new AsyncResult(null, sms, null));

                }

            break;

            }

                                    …….

}

 

mSMSRegistrant 调用到Registrant.java文件中notifyRegistrant方法。

internalNotifyRegistrant 调用sendMessage 方法。

public void  notifyRegistrant(AsyncResult ar)

{

    internalNotifyRegistrant (ar.result, ar.exception);

}

internalNotifyRegistrant 收到消息后将消息发送到消息队列。

    /*package*/ void

    internalNotifyRegistrant (Object result, Throwable exception)

    {

        Handler h = getHandler();

 

        if (h == null) {

            clear();

        } else {

            Message msg = Message.obtain();

 

            msg.what = what;

           

            msg.obj = new AsyncResult(userObj, result, exception);

           

            h.sendMessage(msg);

        }

    }

 

sendMessage 依次调用Handler.java 文件中 sendMessage->sendMessageDelayed-> sendMessageAtTime 在 sendMessageAtTime中将该短信消息加入到 消息队列中。

sent = queue.enqueueMessage(msg, uptimeMillis);

 

    public boolean sendMessageAtTime(Message msg, long uptimeMillis)

    {

        boolean sent = false;

        MessageQueue queue = mQueue;

        if (queue != null) {

            msg.target = this;

            sent = queue.enqueueMessage(msg, uptimeMillis);

        }

        else {

            RuntimeException e = new RuntimeException(

                this + " sendMessageAtTime() called with no mQueue");

            Log.w("Looper", e.getMessage(), e);

        }

        return sent;

}

Looper.java 中loop方法。用于将消息队列中消息dispatch出去。
public static final void loop() {

        Looper me = myLooper();

        MessageQueue queue = me.mQueue;

        while (true) {

            Message msg = queue.next(); // might block

            //if (!me.mRun) {

            //    break;

            //}

            if (msg != null) {

                if (msg.target == null) {

                    // No target is a magic identifier for the quit message.

                    return;

                }

                if (me.mLogging!= null) me.mLogging.println(

                        ">>>>> Dispatching to " + msg.target + " "

                        + msg.callback + ": " + msg.what

                        );

                msg.target.dispatchMessage(msg);

                if (me.mLogging!= null) me.mLogging.println(

                        "<<<<< Finished to    " + msg.target + " "

                        + msg.callback);

                msg.recycle();

            }

        }

    }

 

dispatchMessage函数调用当前handle的消息处理函数。

    public void dispatchMessage(Message msg) {

        if (msg.callback != null) {

            handleCallback(msg);

        } else {

            if (mCallback != null) {

                if (mCallback.handleMessage(msg)) {

                    return;

                }

            }

            handleMessage(msg);

        }

    }

 

调用到smsDispatcher.java中

public void handleMessage(Message msg) {

        AsyncResult ar;

 

        switch (msg.what) {

        case EVENT_NEW_SMS:

        ………

}

}

从而实现了对新收到的短信的处理。


http://blog.csdn.net/tjy1985/article/details/7228294