SoFunction
Updated on 2025-03-11

Android calls the method to send text messages

Android calls the method to send text messages

Function: Call the SMS function

1. Permissions

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

2. Specific implementation

Uri smstoUri = ("smsto:"); 
Intent intent = new Intent(Intent.ACTION_VIEW,smstoUri); 
("address","telephone number"); // If there is no phone number, it is the default, that is, it is empty when displayed.("sms_body","SMS content"); // Set the content sent("-dir/mms-sms"); 
startActivity(intent); 

Activity code:

public class MainActivity extends Activity { 
 
  private EditText phone ,message; 
  private Button sendbtn; 
  protected void onCreate(Bundle savedInstanceState) { 
    (savedInstanceState); 
    setContentView(.activity_main); 
     
    phone = (EditText) findViewById(); 
    message = (EditText) findViewById(); 
    sendbtn = (Button) findViewById(); 
     
    //Click to send a text message    (new OnClickListener() { 
       
      public void onClick(View v) { 
        String p = ().toString(); 
        String m = ().toString(); 
        Uri smstoUri = ("smsto:"); // Resolve the address        Intent intent = new Intent(Intent.ACTION_VIEW,smstoUri); 
        ("address",p); // If there is no phone number, it is the default, that is, it is empty when displayed.        ("sms_body",m); // Set the content sent        ("-dir/mms-sms"); 
        startActivity(intent); 
      } 
    }); 
  } 
} 

Configuration file:

&lt;?xml version="1.0" encoding="utf-8"?&gt; 
&lt;manifest xmlns:andro 
  package="" 
  android:versionCode="1" 
  android:versionName="1.0" &gt; 
 
  &lt;uses-sdk 
    android:minSdkVersion="10" 
    android:targetSdkVersion="10" /&gt; 
 
  &lt;application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" &gt; 
    &lt;activity 
      android:name="" 
      android:label="@string/app_name" &gt; 
      &lt;intent-filter&gt; 
        &lt;action android:name="" /&gt; 
 
        &lt;category android:name="" /&gt; 
      &lt;/intent-filter&gt; 
    &lt;/activity&gt; 
  &lt;/application&gt; 
 
  &lt;!-- Send SMS permission --&gt; 
  &lt;uses-permission android:name=".SEND_SMS" /&gt; 
 
&lt;/manifest&gt; 

Layout diagram:



<RelativeLayout xmlns:andro 
  xmlns:tools="/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:paddingBottom="@dimen/activity_vertical_margin" 
  android:paddingLeft="@dimen/activity_horizontal_margin" 
  android:paddingRight="@dimen/activity_horizontal_margin" 
  android:paddingTop="@dimen/activity_vertical_margin" 
  tools:context=".MainActivity" > 
 
  <EditText 
    android: 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:ems="10" 
    android:inputType="number" > 
 
    <requestFocus /> 
  </EditText> 
 
  <Button 
    android: 
    style="?android:attr/buttonStyleSmall" 
    android:layout_width="150dp" 
    android:layout_height="50dp" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="28dp" 
    android:text="Send" /> 
 
  <EditText 
    android: 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/sendbtn" 
    android:layout_alignLeft="@+id/phone" 
    android:layout_marginBottom="48dp" 
    android:ems="10" /> 
 
</RelativeLayout> 

The above is how Android calls SMS. If you have any questions, please leave a message or go to the community of this site to communicate and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!