Redirecting To Messaging App From Your Android App

By Kunal Kapoor
17-05-2018
407

Sending a default SMS to a specific number through your android application is useful in many applications. Such as when making a customer delivery app, or customer support app, you will be requiring to send custom SMS to users

In this project we will be demonstrating you how to send a SMS to a user with the message and text content pre loaded into the messaging app o the device.


Overview of project:


Design a simple layout containing 3 elements: 2 editText for mobile number, text message input and a button to inititate the SMS send command.


UI: activity_main.xml:

xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ourcoaching.simplesms.MainActivity">


<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SEND SMS"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.805" />

<EditText
android:id="@+id/editText"
android:text="9999999999"
android:layout_width="251dp"
android:layout_height="46dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="phone"
android:maxLength="10"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.581"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.204" />

<EditText
android:id="@+id/editText2"
android:layout_width="251dp"
android:layout_height="120dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="textMultiLine"
android:text="Name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.504"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.469" />


android.support.constraint.ConstraintLayout
>


Instantiate the variables for three UI elements in java code:

editText=(EditText)findViewById(R.id.editText);
button=(Button)findViewById(R.id.button);
editText2=(EditText)findViewById(R.id.editText2);


Define an OnClick listener for the button and create a method for the same:

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SMSFunction();
}
});

 

Inside the SMSFunction() method, retrieve the SMS text and mobile number from respective editText's

String number=editText.getText().toString();
String textSMS=editText2.getText().toString();


Create a URI for the mobile number

Uri sms_uri = Uri.parse("smsto:"+number);

Create an Intent and pass the sms_uri into that Intent

Intent sms_intent = new Intent(Intent.ACTION_SENDTO);
sms_intent.setData(sms_uri);

Put the SMS text(to be send) into Intent 

sms_intent.putExtra("sms_body", textSMS);

Start activity for the Intent to initiate SMS send command

if (sms_intent.resolveActivity(getPackageManager()) != null) {
startActivity(sms_intent);
}else Toast.makeText(this,"There was a problem",Toast.LENGTH_SHORT).show();


Complete Java code: MainActivity.java:

package com.ourcoaching.simplesms;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private Button button;
private EditText editText,editText2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

editText=(EditText)findViewById(R.id.editText);
button=(Button)findViewById(R.id.button);
editText2=(EditText)findViewById(R.id.editText2);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SMSFunction();
}
});
}

private void SMSFunction() {
String number=editText.getText().toString();
String textSMS=editText2.getText().toString();
if (number.length()==10){
if (textSMS!=null){

Uri sms_uri = Uri.parse("smsto:"+number);
Intent sms_intent = new Intent(Intent.ACTION_SENDTO);
sms_intent.setData(sms_uri);
sms_intent.putExtra("sms_body", textSMS);

if (sms_intent.resolveActivity(getPackageManager()) != null) {
startActivity(sms_intent);
}else Toast.makeText(this,"There was a problem",Toast.LENGTH_SHORT).show();

}else {
Toast.makeText(this,"Text Empty",Toast.LENGTH_SHORT).show();
}
}else Toast.makeText(this,"Incorrect mobile number",Toast.LENGTH_SHORT).show();
}
}


Download the code from GitHub



Related Tutorials