SoFunction
Updated on 2025-04-10

Android uses SmsManager to implement SMS sending function

Android uses SmsManager to send text messages for your reference. The specific content is as follows

Key code implementation

package ; 
 
import ; 
 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
 
public class MainActivity extends Activity implements OnClickListener { 
   
  private EditText mNumberText; 
  private EditText mContentText; 
   
  protected void onCreate(Bundle savedInstanceState) { 
    (savedInstanceState); 
    setTitle(getString(.app_name)); 
    setContentView(.activity_main); 
     
    mNumberText = (EditText)();// Find number    mContentText = (EditText)(.msg_content);  
    findViewById().setOnClickListener(this); 
  } 
   
  @Override 
  public void onClick(View arg0) { 
    String content = ().toString(); 
    String number = ().toString(); 
     
    if((number)){ 
      showToast("Please enter your mobile phone number"); 
      return; 
    } 
    if((content)){ 
      showToast("Please enter content"); 
      return; 
    } 
     
    ArrayList<String> messages = ().divideMessage(content); 
    for(String text : messages){ 
      ().sendTextMessage(number, null, text, null, null); 
    } 
    showToast(getString()); 
  } 
   
  private void showToast(String msg) { 
    (, msg, Toast.LENGTH_LONG).show(); 
  } 
   
} 

xml layout file

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:andro 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:background="#87CEEB" 
  android:paddingTop="5dp" > 
 
  <EditText 
    android: 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:hint="Please enter your mobile phone number" 
    android:inputType="number" /> 
 
  <LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:gravity="center_vertical" 
    android:orientation="horizontal" > 
 
    <EditText 
      android: 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_weight="1" 
      android:hint="Please enter the text message" /> 
 
    <Button 
      android: 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:gravity="center_vertical" 
      android:text="@string/button" 
      android:textColor="#FF1493" /> 
  </LinearLayout> 
 
</RelativeLayout> 

Remember permissions

<uses-permission android:name=".SEND_SMS"/>

Attached source code:SMSManager is used to implement SMS sending

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.