轉載 http://blog.csdn.net/tjy1985/article/details/7228294 java
一 初始化 ide
手機開機初始化調用GSMPhone 構造函數。 函數
GSMPhone (Context context, CommandsInterface ci, PhoneNotifier notifier, boolean unitTestMode) oop
建立 mSMS = new GsmSMSDispatcher(this); ui
該類繼承於SMSDispatcher。類SMSDispatcher中構造函數中初始化了 短信的消息 this
mCm.setOnNewSMS(this, EVENT_NEW_SMS, null); spa
mCm.setOnSmsStatus(this, EVENT_NEW_SMS_STATUS_REPORT, null); .net
mCm.setOnIccSmsFull(this, EVENT_ICC_FULL, null); blog
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:
………
}
}
從而實現了對新收到的短信的處理。