tutorial on using MMS module to send and receive single card and dual SIM message in Android development _android

Source: Internet
Author: User
Tags reflection

I. Message delivery:
Com.android.mms.data.WorkingMessage.java Class
Send () function:

public void Send () {   
 
  ...  
 
  if (Requiresmms () | | | Addresscontainsemailtomms (CONV, msgtxt)) {   
 
    //MMS   
 
    slideshow.prepareforsend (); 
 
    New Thread (New Runnable () {public   
 
      void run () {   
 
        sendmmsworker (conv, Mmsuri, Persister, slideshow, sendreq);   
 
    }). Start ();   
 
  } else {   
 
    //SMS   
 
    new Thread (new Runnable () {public   
 
      void run () {   
 
        presendsmsworker (conv, msgtext);   
 
      }   
 
    }). Start ();   
 
  ...... 
 
}  


Prapareforsend (). Make sure there is a slidshow, which is the substance. Make sure the text is copied. Make sure the title. According to message classification, if it is a message directly up a thread, run Presendsmsworker function, send SMS; if it is MMS, run first prapareforsave ensure text information, then a thread, run Sendmmsworker function alone. Whether it's a text message or a MMS, one of the worker functions, even if you send a message, succeeds. Finally modify the recipient cache, reset the flag bit, the process is over.
The SMS sends the Presendsmsworker function first, and then plays the Sendsmsworker function in the Presendsmsworker function.

private void Sendsmsworker (String msgtext, String semiseprecipients, long threadId) { 
 
...  
 
Messagesender sender = new Smsmessagesender (Mcontext, dests, Msgtext, threadId); 
 
Sender.sendmessage (threadId); 
 
......    
 
} 


The Smsmessagesender.java class, under Mms/transaction, implements the Messagesender interface, which SendMessage and returns a Boolean value. Returns true if MMS is being sent. Returns False if SMS is sent.

Of course, for Tanka mobile phone and dual-SIM phone SMS delivery process is different (SMS received the same process, the relative process is also relatively concise), on the specific process or directly using a UML diagram to illustrate more directly:
Sequence diagram for sending and receiving information:
Send SMS
Single card mobile phone message sent by the sequence diagram as shown in the picture:

The sequence diagram of the dual sim phone text message is shown in the following figure:

two. Receipt of SMS
The information receives the work is completes by the bottom, when has a new information when the bottom completes receives the intent way to inform the upper level application, the information related content also includes in intent, The information intent that Android supports are defined in android.provider.Telephony.Intents.

SMS reception, for the upper application is to deal with the broadcast event sms_received_action, it is sent by the frameworks to tell the upper layer that a new SMS has been received. In MMS, which is handled by Privilegedsmsreceiver, it receives sms_received_action (Android.provider.Telephony.Intents.SMS_RECEIVED_ACTION = "Android.provider.Telephony.SMS_RECEIVED") will start Smsreceiverservice to do the specific processing.
Smsreceiverservice will first check the type of SMS, if it is Class0 SMS, directly in the GUI display, do not do any other processing, it will not be stored in the database, and will not be in notification bar to do notification.
For other text messages, either replace the existing message or insert it as a new message. The principle is that if the existing text in the database, and the original address of the new message and the identity of the protocol is the same, then replace it with a new text message, or as a new text message inserted.
The specific replacement process: First use the new text to generate a contentvalues, and then use the address of the message and protocol identification as a condition to the database to query, if found, replace, otherwise stored.
The stored process is also a cotentvalues, and then takes out the thread ID and address of the SMS, which is synchronized with the contact database to ensure that it is an identifiable address. If the thread ID is not legal, attempt to regenerate the thread ID with a synchronized address, try 5 times. The refreshed thread ID is then placed in the Contentvalues, and the contentvalues is inserted into the database. If set to store the information to the SIM card, also call Smsmanager to copy the information to the SIM card. Calculate the size of the SMS and update to the database. Delete expired text messages, and text messages that exceed the number limit, and then return the inserted SMS URI.
Finally, for the replacement or insertion of text messages, use the URI to StatusBar do notification.
The GUI can also get new text messages when refreshing the list, because the text messages are already stored in the database.

The sequence diagram received by SMS is as shown in the picture:

Third, dual SIM card for mobile phone analysis of abnormal SMS and solution
because it is a dual-SIM card, and two slots support operators or network system is not necessarily the same, such as a slot support WCDMA, the other only support GSM, resulting in the normal method of parsing SMS is very easy to encounter anomalies.
Here we look at the solution, which needs to be reflected in the resolution of different types of SMS, and for different models, you need to adjust the appropriate adaptation:

To get message information, note: To solve the dual SIM card to resolve the problem of SMS, using the Java reflection mechanism, the priority to resolve the GSM type of SMS, if the resolution failed to be based on the type of CDMA SMS parsing)

public static smsmessage[] Getsmsmessage (Intent Intent) {smsmessage[] msgs = null; 
    Object messages[] = (object[]) Intent.getserializableextra ("PDUs"); 
    int len = 0; 
      if (Null!= messages && (len = messages.length) > 0) {msgs = new Smsmessage[len]; 
          try {for (int i = 0; i < len; i++) {smsmessage message = NULL; if ("GSM". Equals (Intent.getstringextra ("from")) {//adapter Moto XT800 Dual SIM Phone message = createfrompdugsm ((byte[)) 
          Messages[i]); else if ("CDMA". Equals (Intent.getstringextra ("from")) {//Fit Moto XT800 Dual sim Phone message = CREATEFROMPDUCDMA ( 
          (byte[]) messages[i]); else {message = SMSMESSAGE.CREATEFROMPDU ((byte[]) messages[i]);//system default resolution SMS mode} IF (NULL = = message) 
            {//Solve dual SIM-type mobile phone resolution SMS exception problem message = CREATEFROMPDUGSM ((byte[]) messages[i]); if (null = = message) {message = CREATEFROMPDUCDMA ((byte[]) messages[i]); 
          } if (null!= message) {msgs[i] = message; 
        A catch (Exception e) {e.printstacktrace ()); Msgs = Getsmsmessagebyreflect (Intent); 
        Solution to the dual SIM card for mobile phone to resolve the problem of SMS exception catch (Error er) {er.printstacktrace (); Msgs = Getsmsmessagebyreflect (Intent); 
  Solve the problem of dual SIM card for mobile phone parsing SMS exception problems} return msgs; 

 }

Get text by reflection

/** * Use the Java reflection mechanism to get SMS messages (solve the problem of dual SIM cards to resolve SMS problems, the priority to resolve the GSM type of SMS, if the resolution fails to be based on the type of CDMA SMS parsing) * * @param intent * @return 
    * * private static smsmessage[] Getsmsmessagebyreflect (Intent Intent) {smsmessage[] msgs = null; 
    Object messages[] = (object[]) Intent.getserializableextra ("PDUs"); 
    int len = 0; 
      if (Null!= messages && (len = messages.length) > 0) {msgs = new Smsmessage[len]; 
          try {for (int i = 0; i < len; i++) {Smsmessage message = createfrompdugsm ((byte[)) messages[i]); 
          if (null = = message) {message = CREATEFROMPDUCDMA ((byte[]) messages[i]); 
          } if (null!= message) {msgs[i] = message; 
      A catch (SecurityException e) {e.printstacktrace ()); 
      catch (IllegalArgumentException e) {e.printstacktrace (); 
      catch (ClassNotFoundException e) {e.printstacktrace (); catch (NosuchmeThodexception e) {e.printstacktrace (); 
      catch (Illegalaccessexception e) {e.printstacktrace (); 
      catch (InvocationTargetException e) {e.printstacktrace (); 
      catch (Instantiationexception e) {e.printstacktrace (); 
  } return msgs; 

 }

Analysis of GSM type SMS via Java reflection mechanism:

private static Smsmessage createfrompdugsm (byte[] PDU) throws SecurityException, IllegalArgumentException, ClassNotFoundException, Nosuchmethodexception, Illegalaccessexception, InvocationTargetException, instantiationexception {return 
    CREATEFROMPDU (PDU, "com.android.internal.telephony.gsm.SmsMessage"); 
  } 

Parsing CDMA type of SMS

private static Smsmessage CREATEFROMPDUCDMA (byte[] PDU) throws SecurityException, IllegalArgumentException, ClassNotFoundException, Nosuchmethodexception, Illegalaccessexception, InvocationTargetException, instantiationexception {return 
  CREATEFROMPDU (PDU, "com.android.internal.telephony.cdma.SmsMessage"); 
} 

Analysis of GSM or CDMA type of SMS

private static Smsmessage CREATEFROMPDU (byte[] PDU, String className) throws ClassNotFoundException, SecurityException, Nosuchmethodexception, IllegalArgumentException, Illegalaccessexception, InvocationTargetException, instantiationexception { 
    class<?> clazz = Class.forName (className); 
    Object = Clazz.getmethod ("Createfrompdu", Byte[].class). Invoke (Clazz.newinstance (), PDU); 
    if (null!= object) { 
      constructor<?> constructor = SmsMessage.class.getDeclaredConstructor (Class.forName (" Com.android.internal.telephony.SmsMessageBase ")); 
      Constructor.setaccessible (true); 
      Return (Smsmessage) constructor.newinstance (object); 
    else {return 
      null; 
    } 
  } 

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.