Android 2.3 Texting Details process

Source: Internet
Author: User

In Android, the app Smsmanager.java a series of ways to send text messages, and the content is sent in many ways, such as Sendtextmessage, Sendmultiparttextmessage, Senddatamessage et cetera, in this article we will take one of the examples to explain the complete process of sending text messages, if there is a wrong place, please correct me, learn together.

1. Starting point: Smsmanager.java (Frameworks/base/telephony/java/android/telephony/smsmanager.java)

The core code for Sendtextmessage is as follows:


View Plaincopy to clipboardprint?public void Sendtextmessage (
......
try {
ISms iccisms = ISms.Stub.asInterface (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.asInterface (Servicemanager.getservice ("isms"));
if (iccisms! = null) {
Iccisms.sendtext (destinationaddress, scaddress, text, sentintent, deliveryintent);
}
} catch (RemoteException ex) {
Ignore it
}
Where is the view plaincopy to Clipboardprint? ISms iccisms = ISms.Stub.asInterface (Servicemanager.getservice ("isms"));
ISms iccisms = ISms.Stub.asInterface (Servicemanager.getservice ("isms")), which is obtained by Aidl, and then calls the SendText () method of the service object, So where is this service object?
2. We know that After creating a xx.aidl file in Eclipse, the IDE will automatically generate an interface named Xx.java with the relevant tools, which has an inner class called Stub, and we can create a class and inherit this inner class, and this will enable interprocess communication, which is the knowledge of aidl, which is not detailed here. We look down:

According to the AIDL implementation process, that service object should be inherited by Isms.stub, after finding we found this service class: Iccsmsinterfacemanagerproxy.java, so from Smsmanager.sendtextmessage The () method invokes the SendText () method of the Iccsmsinterfacemanagerproxy object.

3. Phase II: Iccsmsinterfacemanagerproxy.java (frameworks/base/telephony/java/com/android/internal/telephony/ Iccsmsinterfacemanagerproxy)

We look at Iccsmsinterfacemanagerproxy's SendText () method core code:

View Plaincopy to Clipboardprint?private Iccsmsinterfacemanager miccsmsinterfacemanager; 
...... 
public void SendText (string destaddr, string scaddr, 
             String text, pendingintent sentintent, pendingintent deliveryintent) { 
         Miccsmsinterfacemanager.sendtext (destaddr, scaddr, text, sentintent, deliveryintent);  
   } 
Private Iccsmsinterfacemanager Miccsmsinterfacemanager;
......
public void SendText (string destaddr, String scaddr,
             String text, pendingintent sentintent, pendingintent deliveryintent) {
         Miccsmsinterfacemanager.sendtext (destaddr, scaddr, text, sentintent, deliveryintent);
   } The
continues to be called, at this point the SendText () method of the Iccsmsinterfacemanager object is called, what is Iccsmsinterfacemanager??

4. Phase III: Iccsmsinterfacemanager.java (frameworks/base/telephony/java/com/android/internal/telephony/ Iccsmsinterfacemanager.java)

See from the code that Iccsmsinterfacemanager is an abstract class that inherits Isms.stub, the relevant core code is as follows:

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);
}
The SendText () method of the Iccsmsinterfacemanager object calls the SendText () method of the Smsdispatcher class and continues down:
5. Stage four: Smsdispatcher.java (Frameworks/base/telephony/java/com/android/internal/telephony/smsdispatcher.java)

The class is an abstract class, its sendtext () is not implemented, its implementation class is Gsmsmsdispatcher.java or Cdmasmsdispatcher.java, assuming we use the GSM network, The SendText () method to Gsmsmsdispatcher is called at this time.

6. Fifth stage: Gsmsmsdispatcher.java (frameworks/base/telephony/java/com/android/internal/telephony/gsm/ Gsmsmsdispatcher.java)

The core code is as follows:

View Plaincopy to clipboardprint?protected void SendText (String destaddr, String scaddr, string text,
Pendingintent sentintent, pendingintent deliveryintent) {
SMSMESSAGE.SUBMITPDU PDU = SMSMESSAGE.GETSUBMITPDU (
SCADDR, Destaddr, text, (deliveryintent! = null));
SENDRAWPDU (pdu.encodedscaddress, Pdu.encodedmessage, Sentintent, deliveryintent);
}
......
protected void SendText (String destaddr, String scaddr, string text,
Pendingintent sentintent, pendingintent deliveryintent) {
SMSMESSAGE.SUBMITPDU PDU = SMSMESSAGE.GETSUBMITPDU (
SCADDR, Destaddr, text, (deliveryintent! = null));
SENDRAWPDU (pdu.encodedscaddress, Pdu.encodedmessage, Sentintent, deliveryintent);
}
......

Here, "SendText ()" The word is gone, replaced by another method name: SENDRAWPDU (), tracking this method can find it is Smsdispatcher.java a method, this kind of look familiar? Yes, we've dealt with it in the fourth phase! Let's see what the SENDRAWPDU is all about: view plaincopy to clipboardprint?protected void Sendrawpdu (byte[] smsc, byte[) PDU, Pendingintent Sentintent,
Pendingintent deliveryintent) {
......
Sendsms (tracker);
.....
}
protected void Sendrawpdu (byte[] smsc, byte[] PDU, Pendingintent sentintent,
Pendingintent deliveryintent) {
......
Sendsms (tracker);
.....
And a new method name: Sendsms (), the message from SENDRAWPDU () is encapsulated and passed to the Sendsms () method for processing, And in Smsdispatcher.java, this method just declares, its concrete implementation subclass: Gsmsmsdispatcher.java completes. Now let's see Gsmsmsdispatcher.java.
7. Sixth stage: Gsmsmsdispatcher.java (frameworks/base/telephony/java/com/android/internal/telephony/gsm/ Gsmsmsdispatcher.java)

The core code of the Gsmsmsdispatcher.java Sendsms () method is as follows:

View Plaincopy to clipboardprint?protected commandsinterface mCm;

protected void Sendsms (Smstracker tracker) {
HashMap map = Tracker.mdata;

byte smsc[] = (byte[]) map.get ("Smsc");
byte pdu[] = (byte[]) map.get ("PDU");

Message reply = Obtainmessage (Event_send_sms_complete, tracker);
Mcm.sendsms (Iccutils.bytestohexstring (SMSC),
Iccutils.bytestohexstring (PDU), reply);
}
protected Commandsinterface mCm;

protected void Sendsms (Smstracker tracker) {
HashMap map = Tracker.mdata;

byte smsc[] = (byte[]) map.get ("Smsc");
byte pdu[] = (byte[]) map.get ("PDU");

Message reply = Obtainmessage (Event_send_sms_complete, tracker);
Mcm.sendsms (Iccutils.bytestohexstring (SMSC),
Iccutils.bytestohexstring (PDU), reply);
It's not far from success ....
We know that Commandsinterface is a special interface, its ril.java is closely related, and in the above Code sendsms () calls to commandsinterface the object's Sendsms () method to do things, And Commandsiterface is an interface, so things have to be done by its son (actually grandson, Ril's father Basecommands is commandsinterface son) to complete, good, into the Ril.java.

8. Seventh stage: Ril.java (/frameworks/base/telephony/java/com/android/internal/telephony/ril.java)

As long as the study of RIL layer, this thing must be very familiar with, so directly to see its sendsms () method:

View Plaincopy to Clipboardprint?public void Sendsms (String Smscpdu, String PDU, Message result) {
Rilrequest RR
= Rilrequest.obtain (ril_request_send_sms, result);

Rr.mp.writeInt (2);
Rr.mp.writeString (SMSCPDU);
Rr.mp.writeString (PDU);

if (RILJ_LOGD) Riljlog (rr.serialstring () + ">" + requesttostring (rr.mrequest));

Send (RR);
}

Send (Rilrequest RR)
<pre name= "code" class= "java" >private void
Send (Rilrequest RR) {
Message msg;

msg = Msender.obtainmessage (event_send, RR);

Acquirewakelock ();

Msg.sendtotarget ();
}
public void Sendsms (string Smscpdu, String PDU, Message result) {
Rilrequest RR
= Rilrequest.obtain (ril_request_send_sms, result);

Rr.mp.writeInt (2);
Rr.mp.writeString (SMSCPDU);
Rr.mp.writeString (PDU);

if (RILJ_LOGD) Riljlog (rr.serialstring () + ">" + requesttostring (rr.mrequest));

Send (RR);
}

Send (Rilrequest RR)
<pre name= "code" class= "java" >private void
Send (Rilrequest RR) {
Message msg;

msg = Msender.obtainmessage (event_send, RR);

Acquirewakelock ();

Msg.sendtotarget ();
}

ok! in the Sendsms () method, we write the above-mentioned east to parcel, a special rilrequest is sent out, where to send? Then look at: View plaincopy to Clipboardprint?public void
Handlemessage (Message msg) {
Rilrequest rr = (rilrequest) (msg.obj);
Rilrequest req = null;

Switch (msg.what) {
Case Event_send:
Boolean alreadysubtracted = false;
try {
Localsocket s;
......
S.getoutputstream (). write (datalength);
S.getoutputstream (). write (data);
} catch (IOException ex) {
......
}
Break
}
}
public void
Handlemessage (Message msg) {
Rilrequest rr = (rilrequest) (msg.obj);
Rilrequest req = null;

Switch (msg.what) {
Case Event_send:
Boolean alreadysubtracted = false;
try {
Localsocket s;
......
S.getoutputstream (). write (datalength);
S.getoutputstream (). write (data);
} catch (IOException ex) {
......
}
Break
}
} Focus: Localsocket, S.getoutputstream (). Write (data)
We write the message related data and special Rilrequst object to the output stream of the socket, and then pass the data to the RIL layer, that is, the bottom layer, and then the RIL level through the receiving socket in the data parsed to get the request content and processing, to this, the text message of the Java section is finished.

RIL layer after the analysis, if there is no place in the text, please tell me, thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.