Android dual-card dual-waiting mobile phone resolution SMS exception Analysis and Solution

Source: Internet
Author: User

Http://blog.csdn.net/t12x3456


During development, there will inevitably be various adaptation problems, especially those that have been deeply modified and customized, some of which are very different from each other in hardware and software, I have to mention a wonderful model here. That's right, it's a dual-card dual-waiting mobile phone (for example ). because it is a dual SIM card, and the two card slots support different carriers or network standards, for example, one card slot supports W, it is easy to encounter exceptions when parsing text messages using the normal method


If you want to learn more about the process of sending and receiving dual-card dual-waiting SMS messages, please refer to my previous analysis on MMS:

Android SMS module analysis (4) sending and receiving of MMS messages


Here, let's take a look at the solution. here we need to parse different types of text messages in reflection mode, and adjust and adapt different models accordingly:


Get SMS information. Note: To solve the problem of SMS parsing exceptions on Dual-card dual-Wait mobile phones, use the Java reflection mechanism to parse GSM-type SMS messages first. If the parsing fails, the CDMA-type SMS will be parsed)

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 ++) {smessage message = null; if ("GSM ". equals (intent. getStringExtra ("from") {// message = createFromPduGsm (byte []) messages [I]);} else if ("CDMA ". equals (intent. getStringExtra ("from") {// applicable to MOTO XT800 dual-card dual-waiting mobile phone message = createFromPduCdma (byte []) messages [I]);} else {message = SmsMessage. createFromPdu (byte []) messages [I]); // the system's default SMS resolution method} if (null = message) {// solves the problem of SMS parsing exceptions for dual-card and dual-to-use mobile phones. message = createFromPduGsm (byte []) messages [I]); if (null = message) {message = createFromPduCdma (byte []) messages [I]) ;}} if (null! = Message) {msgs [I] = message ;}} catch (Exception e) {e. printStackTrace (); msgs = getsmessagebyreflect (intent); // solves the problem of SMS parsing exceptions on Dual-card dual-Wait mobile phones} catch (Error er) {er. printStackTrace (); msgs = getsmessagebyreflect (intent); // solves the problem of SMS parsing exceptions on Dual-card dual-Wait mobile phones} return msgs ;}


Retrieve SMS Using Reflection

/*** Use the Java reflection mechanism to obtain text message information (to solve the problem of SMS parsing exceptions on Dual-card dual-Wait mobile phones, the GSM-type text message is first parsed. If the parsing fails, the CDMA-type text message is parsed) ** @ param intent * @ return */private static SmsMessage [] getsmessagebyreflect (Intent intent) {smessage [] 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 ;}} 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 ;}



Parse GSM text messages using the 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");}

Parse CDMA text messages

private static SmsMessage createFromPduCdma(byte[] pdu) throws SecurityException, IllegalArgumentException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {return createFromPdu(pdu, "com.android.internal.telephony.cdma.SmsMessage");}

Parse SMS messages of the GSM or CDMA type

private static SmsMessage createFromPdu(byte[] pdu, String className) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException {Class<?> clazz = Class.forName(className);Object 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;}}


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.