This article describes the Android implementation of dual-mode (CDMA/GSM) mobile phone SMS listening method. Share to everyone for your reference, specific as follows:
First, the problem analysis:
Recently in doing a short message to start the application of the function, to use SMS listening, the code is as follows:
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.telephony.SmsMessage;
Import Android.widget.Toast;
public class Smsreceiver extends broadcastreceiver{
/* When a message is received, this method is triggered
/public void OnReceive. Intent Intent) {
Bundle Bundle = Intent.getextras ();
if (Bundle!=null && bundle.get ("PDUs")!=null) {
object[] PDUs = (object[)) bundle.get ("PDUs");
Get an Array object consisting of SMS content
if (pdus!=null && pdus.length>0) {
smsmessage[] messages = new smsmessage[ Pdus.length];
for (int i=0;i<pdus.length;i++) {
byte[] PDU = (byte[]) pdus[i];
Get the message content, the content is in the PDU format storage
messages[i] = SMSMESSAGE.CREATEFROMPDU (PDU);
}
for (Smsmessage msg:messages) {
string smscontent = Msg.getmessagebody ();//Get SMS content
String smssender = Msg.getoriginatingaddress (); Get SMS Sender's phone number}}}
The actual application found that the dual-mode mobile phone received when the message processing is always in the SMSMESSAGE.CREATEFROMPDU where the exception, exception information:
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)
And in the Android source can see the Createfrompdu method:
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.database.ContentObserver;
Import Android.database.Cursor;
Import Android.net.Uri;
Import Android.os.Handler; public class Smsreceiver extends Broadcastreceiver {private static final String sms_received = "Android.provider.Telephon
Y.sms_received ";
Private context M_context;
Private Smscontentobserver M_smsobserver = new Smscontentobserver (new Handler ()); @Override public void OnReceive (context context, Intent Intent) {this.m_context = context; if (Intent.getaction (). Equals ( sms_received) {//register SMS Change monitor context.getcontentresolver (). Registercontentobserver (Uri.parse ("content://sms/"), True
, M_smsobserver); }/** * SMS content Observer * @author Sinber */Private class Smscontentobserver extends contentobserver{public smscontentobserve R (Handler Handler) {super (Handler);}/** * @Description when a message table sends a change, calling the method * requires two permissions *<li>android.permission.rea D_sms Read SMS </li> *≪li>android.permission.write_sms Write SMS </li> * @Author SINEBR * */@Override public void OnChange (Boolean Selfcha
Nge) {super.onchange (selfchange);
Cursor Cursor = null; try{//Read SMS in Inbox cursor = M_context.getcontentresolver (). Query (Uri.parse ("Content://sms/inbox"), NULL, NULL, NULL, "
Date desc ");
String body;
Boolean hasdone = false; if (cursor!= null) {while (Cursor.movetonext ()) {BODY = cursor.getstring (Cursor.getcolumnindex (' body ')); if (Body!= null
&& body.equals ("" "startmyactivity")) {//Here omit the code that initiates the application Hasdone = true; break;} if (Hasdone) {break;}} }}catch (Exception e) {e.printstacktrace ();}
finally{if (cursor!=null) Cursor.close ();}}
}
}
If it is a dual-mode mobile phone, call this method will produce errors, the problem is the source of the Telephonymanager.getdefault (). Getphonetype (); The return value of the method does not correspond to the type of the dual-mode phone. The native Android system does not support dual-mode handsets.
Ii. Solutions:
We can use the broadcast receiver and content observer, the way to directly read the phone's SMS database, so as to avoid the wrong production, nonsense is not more said, directly on the code:
Import Android.content.BroadcastReceiver;
Import Android.content.Context;
Import android.content.Intent;
Import Android.database.ContentObserver;
Import Android.database.Cursor;
Import Android.net.Uri;
Import Android.os.Handler; public class Smsreceiver extends Broadcastreceiver {private static final String sms_received = "Android.provider.Telephon
Y.sms_received ";
Private context M_context;
Private Smscontentobserver M_smsobserver = new Smscontentobserver (new Handler ()); @Override public void OnReceive (context context, Intent Intent) {this.m_context = context; if (Intent.getaction (). Equals ( sms_received) {//register SMS Change monitor context.getcontentresolver (). Registercontentobserver (Uri.parse ("content://sms/"), True
, M_smsobserver); }/** * SMS content Observer * @author Sinber */Private class Smscontentobserver extends contentobserver{public smscontentobserve R (Handler Handler) {super (Handler);}/** * @Description when a message table sends a change, calling the method * requires two permissions * <li>android.permiss Ion. Read_sms Read SMS </li> * <li>android.permission.write_sms Write SMS </li> * @Author SINEBR * */@Override public void OnChange (bo
Olean selfchange) {super.onchange (selfchange);
Cursor Cursor = null; try{//Read SMS in Inbox cursor = M_context.getcontentresolver (). Query (Uri.parse ("Content://sms/inbox"), NULL, NULL, NULL, "
Date desc ");
String body;
Boolean hasdone = false; if (cursor!= null) {while (Cursor.movetonext ()) {BODY = cursor.getstring (Cursor.getcolumnindex (' body ')); if (Body!= null
&& body.equals ("" "startmyactivity")) {//Here omit the code that initiates the application Hasdone = true; break;} if (Hasdone) {break;}} }}catch (Exception e) {e.printstacktrace ();}
finally{if (cursor!=null) Cursor.close ();}}
}
}
Finally, don't forget to add the appropriate permissions to the Androidmanifest.xml.
<!--receive SMS permission-->
<uses-permission android:name= "Android.permission.RECEIVE_SMS"/>
<!-- Send SMS Permission-->
<uses-permission android:name= "Android.permission.SEND_SMS"/>
And don't forget to register the broadcast receiver:
<receiver android:name= ". Smsreceiver ">
<intent-filter>
<action android:name=" Android.provider.Telephony.SMS_RECEIVED "/>
</intent-filter>
</receiver>
This will be able to adapt to all of the Android phone, whether it is dual-mode or single mode is no problem, the problem is solved.
For more information on Android-related content readers can view the site topics: "Android Database Operating skills summary", "Android programming activity Operation Skills Summary", "Android File Operation skills Summary", " Android programming development of the SD card operation method Summary, "Android Development introduction and Advanced Course", "Android Resource Operation skills Summary", "Android View tips Summary" and "Android Control usage Summary"
I hope this article will help you with the Android program.