Create Android app to send sms from android mobile

15/04/2017   03:40:49 PM

Hello in this android article I explain how to create your own Android application which is specially used to send sms from android mobile to other android mobile using android.telephony.SmsManager

What is android.telephony.SmsManager?

This is predefined class in android which,manages SMS operations such as sending data, text messages.

Get this object by calling the static method SmsManager.getDefault().

1. Create a new project in android studio
 
2. Add the following code in manifest file
//permission to send sms  
android:name="android.permission.SEND_SMS" />
 
android app to send sms
 

3.Add the code in Res/layout/activity_main.xml

<?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: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="com.xperiaitinnovations.smssending.MainActivity">
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/etphoneno"
        android:hint="Enter Phone Number"
        android:phoneNumber="true"
        android:textColorHint="@color/abc_primary_text_material_dark"
        android:layout_centerHorizontal="true" />
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/etmessage"
        android:layout_below="@+id/etphoneno"
        android:layout_alignLeft="@+id/etphoneno"
        android:layout_alignStart="@+id/etphoneno"
        android:textColorHint="@color/abc_primary_text_material_dark"
        android:hint="Enter SMS" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send Sms"
        android:id="@+id/btnsms"
        android:layout_below="@+id/etmessage"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="48dp" />
</RelativeLayout>

 

4. Add following code in MainActivity.java 
 
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity {
 
    Button sendsmsbtn;
    EditText  phoneno;
    EditText  message;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sendsmsbtn = (Button) findViewById(R.id.btnsms);
        phoneno = (EditText) findViewById(R.id.etphoneno);
        message = (EditText) findViewById(R.id.etmessage);
 
        sendsmsbtn.setOnClickListener(new View.OnClickListener() {
 
        public void onClick(View view) {
 
                sendSMSMessage();
            }
        });
    }
 
    protected void sendSMSMessage() {
 
        String PhoneNo = phoneno.getText().toString();
        String Message = message.getText().toString();
 
        try {
              SmsManager smsManager = SmsManager.getDefault();
              smsManager.sendTextMessage(PhoneNo, null, Message, null, null);
             Toast.makeText(getApplicationContext(), "SMS sent.", Toast.LENGTH_LONG).show();
           }
 
        catch (Exception e) {
 
            Toast.makeText(getApplicationContext(), "SMS faild to sent,try again.", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }
}
 

Output

                   

Support us by Sharing