SoFunction
Updated on 2025-04-10

Detailed explanation of the sending and broadcasting of Android SMS to realize the monitoring of SMS

This article introduces the sending of Android SMS and the monitoring of SMS by broadcast receivers. Pay attention to the permission settings in the Android list and the implementation of broadcast registration monitoring. Without further ado, the code is as follows:

The following is the XML of the Android manifest


<manifest xmlns:andro
  package=""
  android:versionCode="1"
  android:versionName="1.0" >

  <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

  <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
      android:name=".SendSMS"
      android:label="@string/title_activity_send_sms" >
      <intent-filter>
        <action android:name="" />

        <category android:name="" />
      </intent-filter>
      
    </activity>
    <receiver android:name=".SMSBroadcastReceiver">
      <intent-filter android:priority="1000">
        <action android:name=".SMS_RECEIVED"/>
      </intent-filter>
    </receiver>
    
  </application>
  <uses-permission android:name=".SEND_SMS"></uses-permission><!--Add permissions-->
  <uses-permission android:name=".RECEIVE_SMS"></uses-permission> 
  <uses-permission android:name=".READ_SMS"></uses-permission> 
</manifest>

The main interface for sending short messages and implementing Activity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  android:padding="10sp" >

  <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="phone number" />

  <EditText
    android:
    android:numeric="integer"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Please enter your phone number" />

  <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="SMS content" />

  <EditText
    android:
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Please enter the text message"
    android:lines="3" />

  <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
  </TextView>

  <Button
    android:
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:paddingTop="20sp"
    android:text="sending a text message" 
    android:onClick="send"
    />
 

</LinearLayout>

Implement Activity

package ;

import ;

import ;
import ;
import ;
import ;
import ;
import ;

public class SendSMS extends Activity{
  private EditText num;
  private EditText content;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView();
    num=(EditText) findViewById();
    content=(EditText) findViewById();
  }
  
  public void send(View view ) {
    String strNo=().toString();
    String strContent=().toString();
    
    SmsManager smsManager = ();
    //If the number of words exceeds 5, it needs to be split into multiple text messages to send    if (() > 5) {
      ArrayList<String> msgs = (strContent);
      for (String msg : msgs) {
        (strNo, null, msg, null, null);            
      }
    } else {
      (strNo, null, strContent, null, null);
    }
    ("");
    ("");
    
    (, "SMS sending is completed", Toast.LENGTH_LONG).show();
  }
}

Broadcast receivers realize monitoring of SMS

package ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

public class SMSBroadcastReceiver extends BroadcastReceiver {
  public void onReceive(Context context, Intent intent) {
    SmsMessage msg = null;
      Bundle bundle = ();
      if (bundle != null) {
        Object[] pdusObj = (Object[]) ("pdus");
        for (Object p : pdusObj) {
          msg= ((byte[]) p);
          
          String msgTxt =();//Get the content of the message          
          Date date = new Date(());//time          SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          String receiveTime = (date);
          
          String senderNumber = ();
        
        if (("Testing!")) {
          (context, "success!", Toast.LENGTH_LONG)
              .show();
          ("success!");
          return;
        } else {
          (context, msgTxt, Toast.LENGTH_LONG).show();
          ("Send by:"+senderNumber+"SMS content:"+msgTxt+"Accepted time:"+receiveTime);
          return;
        }
      }
      return;
    }
  }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.