android 短信的接收和发送处理

 

一 初始化

 

手机开机初始化调用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:

        ………

}

}

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值