Method for Android to realize dual-mode (CDMA / GSM) mobile phone short message monitoring

This paper describes the method of Android to realize dual-mode (CDMA / GSM) mobile phone short message monitoring. Share with you for your reference, as follows:

1、 Problem analysis:

Recently, we are working on a function of remotely starting the application through SMS, which requires SMS monitoring. The code is as follows:

In practical application, it is found that an exception always occurs in smsmessage.createfrompdu when the dual-mode mobile phone processes the received SMS. The exception information is:

java.lang.OutOfMemoryError: array size too large at com.android.internal.telephony.cdma.SmsMessage.parsePdu(SmsMessage.java:658) at com.android.internal.telephony.cdma.SmsMessage.createFromPdu(SmsMessage.java:116) at android.telephony.SmsMessage.createFromPdu(SmsMessage.java:162)

In the Android source code, you can see the createfrompdu method:

If it is a dual-mode mobile phone, an error will occur when calling this method. The problem lies in the source code's telephonymanager. Getdefault(). Getphonetype(); The return value of this method has no corresponding dual-mode mobile phone type, and the native Android system does not support dual-mode mobile phones.

2、 Solution:

We can use the combination of broadcast receiver and content observer to directly read the SMS database of the mobile phone, so as to avoid errors, stop talking nonsense and directly write the code:

Finally, don't forget to add corresponding permissions in androidmanifest.xml,

And don't forget to register for broadcast recipients:

In this way, it can adapt to all Android phones. There is no problem whether it is dual-mode or single-mode. The problem is solved.

For more Android related content, readers who are interested can view the topics on this site: summary of Android database operation skills, summary of activity operation skills of Android programming, summary of Android file operation skills, summary of SD card operation methods of Android programming and development, introduction and advanced tutorial of Android development, summary of Android resource operation skills Android view skills summary and Android control usage summary

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>