android簡訊的接收和發送處理

來源:互聯網
上載者:User

標籤:android   style   blog   java   http   tar   

一 初始化

手機開機初始化調用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:

………

}

}

從而實現了對新收到的簡訊的處理。 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.