Detailed process of Android 2.3 text message

Source: Internet
Author: User

In Android, the app uses smsmanager. java provides a series of methods to send text messages, and there are many types of content to be sent, such as sendtextmessage, sendmultiparttextmessage, and senddatamessage, in this article, we will take one of them as an example to describe the complete process of sending text messages. If there are any mistakes, please correct them and learn together.

1. starting point: smsmanager. Java (frameworks/base/telephony/Java/Android/telephony/smsmanager. Java)

The core code of sendtextmessage is as follows:

 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,

ISms iccISms = ISms.Stub.asInterface(ServiceManager.getService("isms"));

The aidl method is used to obtain the service and then call the sendtext () method of the service object. Where is the service object?

2. as we know, create a XX in eclipse. after the aidl file is created, the IDE automatically generates a file named XX using related tools. java interface, which has an internal class named stub. If we create a class and inherit the internal class, we can implement inter-process communication. This is aidl knowledge, this is not detailed here. Let's look down:

According to the aidl implementation process, the service object should inherit isms. stub, after searching, we found this service class: iccsmsinterfacemanagerproxy. java, so from smsmanager. the sendtextmessage () method calls the sendtext () method of the iccsmsinterfacemanagerproxy object.

3. Phase 2: iccsmsinterfacemanagerproxy. Java (frameworks/base/telephony/Java/COM/Android/Internal/telephony/iccsmsinterfacemanagerproxy)

Let's take a look at the core code of the sendtext () method of iccsmsinterfacemanagerproxy:

private IccSmsInterfaceManager mIccSmsInterfaceManager;......public void sendText(String destAddr, String scAddr,            String text, PendingIntent sentIntent, PendingIntent deliveryIntent) {        mIccSmsInterfaceManager.sendText(destAddr, scAddr, text, sentIntent, deliveryIntent);    }

Continue to call. In this case, the sendtext () method of the iccsmsinterfacemanager object is called. What is iccsmsinterfacemanager ??

4. Stage 3: iccsmsinterfacemanager. Java (frameworks/base/telephony/Java/COM/Android/Internal/telephony/iccsmsinterfacemanager. Java)

The Code shows that iccsmsinterfacemanager is an abstract class that inherits isms. stub. The core code is as follows:

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:

5. Stage 4: smsdispatcher. Java (frameworks/base/telephony/Java/COM/Android/Internal/telephony/smsdispatcher. Java)

This class is an abstract class, and its sendtext () is not implemented. Its implementation class is gsmsmsdispatcher. java or cdmasmsdispatcher. java. If we use a GSM network, the sendtext () method of the GSM smsdispatcher is called.

6. Stage 5: gsmsmsdispatcher. Java (frameworks/base/telephony/Java/COM/Android/Internal/telephony/GSM/gsmsmsdispatcher. Java)

The core code is as follows:

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);    }......

At this point, the word "sendtext ()" is gone, and it is replaced by another method name: sendrawpdu (). Tracing this method can find that it is smsdispatcher. is this class familiar with a Java method? Yes. We have already dealt with it in Stage 4! Let's take a look at what sendrawpdu does:

protected void sendRawPdu(byte[] smsc, byte[] pdu, PendingIntent sentIntent,            PendingIntent deliveryIntent) {           ......           sendSms(tracker);           ..... }

Another new method is sendsms (). The information sent from sendrawpdu () is encapsulated and transmitted to the sendsms () method for processing. in Java, this method just declares that its implementation is defined by the sub-class: gsmsmsdispatcher. java is complete. Let's take a look at gsmsmsdispatcher. java.

7. Stage 6: gsmsmsdispatcher. Java (frameworks/base/telephony/Java/COM/Android/Internal/telephony/GSM/gsmsmsdispatcher. Java)

The core code of the GSM smsdispatcher. Java sendsms () method is as follows:

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);    }

Not far from success ....

We know that commandsinterface is a special interface, its RIL. java is closely related. In the code above, sendsms () calls the sendsms () method of the commandsinterface object to do things, while commandsiterface is an interface, therefore, the task had to be completed by its son (in fact, Sun Tzu, rIL's father basecommands is the son of commandsinterface. java.

8. Stage 7: RIL. Java (/frameworks/base/telephony/Java/COM/Android/Internal/telephony/RIL. Java)

As long as you have studied the RIL layer, you must be familiar with it, so you can directly look at its sendsms () method:

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)
private void    send(RILRequest rr) {        Message msg;        msg = mSender.obtainMessage(EVENT_SEND, rr);        acquireWakeLock();        msg.sendToTarget();    }

OK! In the sendsms () method, we write the uploaded data to parcel. When a special rilrequest is sent, where is it sent? Next, let's see:

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;            }        }

Key points: localsocket, S. getoutputstream (). Write (data)

We write text message-related data and special rilrequst objects into the socket output stream, and then transmit the data to the RIL layer, that is, the underlying layer, then, the RIL layer parses the data transmitted from the socket to obtain and process the request content. At this point, the Java section of the text message is complete.

The RIL layer will be analyzed later. If there is something wrong with this article, please let me know. 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.