Android SMS receiving and sending

Source: Internet
Author: User

1. Initialization

Call the GSMPhone constructor when the mobile phone is started.

GSMPhone (Context context, CommandsInterface ci, PhoneNotifier notifier, boolean unitTestMode)

Create mSMS = new GsmSMSDispatcher (this );

This class inherits from SMSDispatcher. The constructor in the SMSDispatcher class initializes messages of short messages.

MCm. setOnNewSMS (this, EVENT_NEW_SMS, null );

MCm. setOnSmsStatus (this, EVENT_NEW_SMS_STATUS_REPORT, null );

MCm. setOnIccSmsFull (this, EVENT_ICC_FULL, null );

HandleMessage defines the message processing function of sms.

Public void handleMessage (Message msg ){

......

Case EVENT_NEW_SMS:

......

}

MSimSmsIntManager = new SimSmsInterfaceManager (this );

SimSmsInterfaceManager inherits from IccSmsInterfaceManager to Isms. stub implementation class.

The IccSmsInterfaceManager class implements the methods defined in Isms. adil. To implement remote calls.

 

2. Text message sending

SMS call interface

SmsManager sms = SmsManager. getDefault ();

Sms. sendTextMessage (string1, null, string2, p, null );

SendTextMessage calls sendRawPdu.

Created in sendRawPdu

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

Instantiate IccSmsInterfaceManager through the aidl interface. Call smsDispatcher. java

Protected void sendRawPdu (byte [] smsc, byte [] pdu, PendingIntent sentIntent,

PendingIntent deliveryIntent) function.

Then the sendSMS interface is called through the stub interface.

SendSMS in RIL. java

Public void

SendSMS (String smscPDU, String pdu, Message result ){

RILRequest rr

= RILRequest. obtain (RIL_REQUEST_SEND_SMS, result );

.....

Send (rr );

}

Send the Request of RIL_REQUEST_SEND_SMS to the rild layer.

The relationship between the Rild and modem is similar to that of other applications.

 

3. receive SMS

There are no duplicates between Modem and rild. The message is received from the ril layer.

In Ril. java, rilcycler receives the SMS message processResponse (p ).

Call the processUnsolicited function.

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

If (mSMSRegistrant! = Null ){

MSMSRegistrant

. NotifyRegistrant (new AsyncResult (null, sms, null ));

}

Break;

}

.......

}

 

MSMSRegistrant calls the yyregistrant method in the Registrant. java file.

Internalpolicyregistrant calls the sendMessage method.

Public void policyregistrant (AsyncResult ar)

{

Internalpolicyregistrant (ar. result, ar. exception );

}

Internalpolicyregistrant sends the message to the Message Queue after receiving the message.

/* Package */void

Internalpolicyregistrant (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 calls sendMessage> sendMessageDelayed> sendMessageAtTime In the Handler. java file in sequence to add the text message to the message queue in sendMessageAtTime.

Sent = queue. enqueueMessage (msg, uptimeMillis );

 

Public boolean sendMessageAtTime (Message msg, long uptimeMillis)

{

Boolean sent = false;

MessageQueue queue = mQueue;

If (queue! = Null ){

Msg.tar get = this;

Sent = queue. enqueueMessage (msg, uptimeMillis );

}

Else {

RuntimeException e = new RuntimeException (

This + "sendMessageAtTime () called with no mQueue ");

Log. w ("logoff", e. getMessage (), e );

}

Return sent;

}

Logoff. java loop method. Dispatch messages in a message queue.
Public static final void loop (){

Lodomainme = mylodomain ();

MessageQueue queue = me. mQueue;

While (true ){

Message msg = queue. next (); // might block

// If (! Me. mRun ){

// Break;

//}

If (msg! = Null ){

If (msg.tar get = null ){

// No target is a magic identifier for the quit message.

Return;

}

If (me. mLogging! = Null) me. mLogging. println (

">>>>> Dispatching to" + msg.tar get + ""

+ Msg. callback + ":" + msg. what

);

Msg.tar get. dispatchMessage (msg );

If (me. mLogging! = Null) me. mLogging. println (

"<Finished to" + msg.tar get + ""

+ Msg. callback );

Msg. recycle ();

}

}

}

 

The dispatchMessage function calls the message processing function of the current handle.

Public void dispatchMessage (Message msg ){

If (msg. callback! = Null ){

HandleCallback (msg );

} Else {

If (mCallback! = Null ){

If (mCallback. handleMessage (msg )){

Return;

}

}

HandleMessage (msg );

}

}

 

Called to smsDispatcher. java

Public void handleMessage (Message msg ){

AsyncResult ar;

 

Switch (msg. what ){

Case EVENT_NEW_SMS:

.........

}

}

This allows you to process newly received text messages.

Related Article

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.