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.asInte易做图ce(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.asInte易做图ce(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.asInte易做图ce(ServiceManager.getService("isms"));
ISms iccISms = ISms.Stub.asInte易做图ce(ServiceManager.getService("isms"));是通过AIDL的方式,获得服务,再调用这个服务对象的sendText()方法,那这个服务对象在哪里呢?
2. 我们知道,在eclipse中创建一个xx.aidl文件后,IDE会利用相关工具自动生成一个名为xx.java的接口,它有一个名为Stub的内部类,那我们自己创建一个类并继承这个内部类,则可以实现了进程间的通信,这个是aidl的知识,这儿不详述。我们往下看:
根据aidl的实现流程,那该服务对象应该是继承了ISms.Stub,经过查找我们发现这个服务类:IccSmsInte易做图ceManagerProxy.java,所以从SmsManager.sendTextMessage()方法调用了IccSmsInte易做图ceManagerProxy对象的sendText()方法。
3. 第二阶段:IccSmsInte易做图ceManagerProxy.java(frameworks/base/telephony/java/com/android/internal/telephony/IccSmsInte易做图ceManagerProxy)
我们看IccSmsInte易做图ceManagerProxy的sendText()方法核心代码:
view plaincopy to clipboardprint?private IccSmsInte易做图ceManager mIccSmsInte易做图ceManager;
......
public void sendText(String destAddr, String scAddr,
String text, PendingIntent sentIntent, PendingIntent deliveryIntent) {
mIccSmsInte易做图ceManager.sendText(destAddr, scAddr, text, sentIntent, deliveryIntent);
}
private IccSmsInte易做图ceManager mIccSmsInte易做图ceManager;
......
public void sendText(String destAddr, String scAddr,
String text, PendingIntent sentIntent, PendingIntent deliveryIntent) {
mIccSmsInte易做图ceManager.sendText(destAddr, scAddr, text, sentIntent, deliveryIntent);
}
继续调用,此时调用的是IccSmsInte易做图ceManager对象的sendText()方法,那IccSmsInte易做图ceManager是什么玩意??
4. 第三阶段:IccSmsInte易做图ceManager.java(frameworks/base/telephony/java/com/android/internal/telephony/IccSmsInte易做图ceManager.java)
从代码看出IccSmsInte易做图ceManager是一个继承了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);
}
IccSmsInte易做图ceManager对象的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/inte
补充:移动开发 , Android ,