Android

Android Sending SMS

Android Sending SMS

Android provides a convenient way to send SMS with the help of its built-in API SmsManager. To use the API, you need to define it using the following code snippet:

SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("number", null, "This is the sms message that you put here.", null, null);

Let’s see how you can use the SmsManager object for sending an SMS on Android. To implement this, you need to have a SIM inserted in your android smartphone or a number equipped in your Android Emulator. 

The first thing that you need to do is to create an Android application with the help of Android Studio IDE with the name greatlearning which will provide the package name com.firstapp.greatlearning. 
After that, you need to modify the MainActivity.java file in your project source folder. See the code snippet below:

package com.firstapp.greatlearning;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      Button onButton = (Button) findViewById(R.id.button);
      onButton.setOnClickListener(new View.OnClickListener() {
         public void onClick(View view) {
            sendSMS();
         }
      });
   }
   
   protected void sendSMS() {
      Log.i("Send SMS in Android", "");
      Intent sendSms = new Intent(Intent.ACTION_VIEW);
      
      sendSms.setData(Uri.parse("smsto:"));
      sendSms.setType("vnd.android-dir/mms-sms");
      sendSms.putExtra("address", new String ("Address-Here"));
      sendSms.putExtra("sms_body", "Type your message here ");
      
      try {
         startActivity(sendSms);
         finish();
         Log.i("SMS sent successfully!!", "");
      } catch (android.content.ActivityNotFoundException ex) {
         Toast.makeText(MainActivity.this, 
         "SMS is not sent, please try later.", Toast.LENGTH_SHORT).show();
      }
   }
   
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }
}

Next, you will need to modify the layout XML file that comes with the name activity_main.xml where you can also add buttons, textfields, and other components. See the code shown below for your reference: 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent" 
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" 
   tools:context=".MainActivity">
   
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Drag and Drop Example"
      android:id="@+id/textView"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:textSize="25dp" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Great Learning"
      android:id="@+id/textView2"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true"
      android:textSize="25dp"
      android:textColor="#ffffff" />
      
   <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageView"
      android:layout_marginTop="52dp"
      android:layout_below="@+id/textView2"
      android:layout_centerHorizontal="true" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Compose SMS"
      android:id="@+id/button"
      android:layout_below="@+id/imageView"
      android:layout_alignRight="@+id/textView2"
      android:layout_alignEnd="@+id/textView2"
      android:layout_marginTop="60dp"
      android:layout_alignLeft="@+id/imageView"
      android:layout_alignStart="@+id/imageView" />
      
</RelativeLayout>

We also need to modify the strings.xml file under our resources folder with the following constants:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">GreatLearningSMSExample</string>
</resources>

Later you need to modify the AndroidManifest.xml file and run the application in Android Emulator. The following XML code shows the default content in our AndroidManifest.xml file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package= "com.firstapp.greatlearning" >
      
   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name="com.firstapp.greatlearning.MainActivity"
         android:label="@string/app_name" >
         
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
         
      </activity>
      
   </application>
</manifest>

This way you can run the application after doing all these configurations, which will ask you to compose a message with a send button. You can also modify the output if you are not satisfied with the GUI, but first, you should check the working of the application. 
 

Top course recommendations for you

    Data Structures & Algorithms in Java
    4 hrs
    Beginner
    165.1K+ Learners
    4.47  (2737)
    Java Programming
    2 hrs
    Beginner
    620.6K+ Learners
    4.48  (40890)
    Python Fundamentals for Beginners
    4 hrs
    Beginner
    707.5K+ Learners
    4.55  (39248)
    Front End Development - HTML
    2 hrs
    Beginner
    495K+ Learners
    4.51  (37240)
    Front End Development - CSS
    2 hrs
    Beginner
    181.5K+ Learners
    4.51  (13158)
    Blockchain Basics
    3 hrs
    Beginner
    82.2K+ Learners
    4.54  (4155)
    Data Structures in C
    2 hrs
    Beginner
    175.2K+ Learners
    4.4  (10410)
    Introduction to R
    1 hrs
    Beginner
    161.9K+ Learners
    4.57  (8167)
    Excel for Beginners
    5 hrs
    Beginner
    1.2M+ Learners
    4.49  (61428)
    Excel for Intermediate Level
    3 hrs
    Intermediate
    208.5K+ Learners
    4.55  (10672)