Hire the author: Zablon M

Introduction

In Android Studio, you can use the auto-reply project to automatically respond to texts or phone calls with pre-written messages. This project would benefit anyone who cannot always respond to texts or phone calls. You can personalize your responses for different contacts and also set up scheduled messages. With this project, you can set up auto-replies for any situation, making it easy to stay in touch with your contacts.

Furthermore, the SMS auto-reply app will listen to incoming messages and reply to the message received as per the data in the backend.

To develop an Android application, you need be familiar with programming languages such as Java, Kotlin, and JavaScript for React Native apps, and Dart for Flutter apps.

Check out the code here.

Step-by-step Procedure

Before getting started, you must have Android Studio installed on your computer and have knowledge of Java.

Step 1: Adding manifest permissions

To send and receive SMS messages, you should add and request two permissions: send_sms and receive_sms. You’ll need to add those permissions to your manifest file. To use the BroadcastReceiver class, you should add a receiver tag to the manifest file.

Below is the manifest file that includes permissions and a receiver tag.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="autoreply.app">
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Smsmessages_auto_reply">
<receiver android:name=".Receive_Sms"
android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Step 2: Customizing the theme file to remove the action bar

To remove the action bar from your activity, you have to customize the theme file. You can do this by adding the following code to your theme file.

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.Smsmessages_auto_reply" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
</resources>
view raw themes.xml hosted with ❤ by GitHub

Step 3: Customizing the file with color values to create color labels

You can customize the file with color values to create your own color labels. You can edit the color codes and color labels as shown in the code below.

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="colorPrimary">#198CE7</color>
<color name="bg">#DF6609</color>
<color name="stroke_bg">#e7008a</color>
<color name="link">#221788</color>
<color name="bg_2">#FF6200EE</color>
</resources>
view raw colors.xml hosted with ❤ by GitHub

Step 4: Adding a toolbar by editing activity_main.xml

A toolbar is a view in an Android app that is typically used to display the app’s title, subtitle, and actions. You can configure it to display different action icons or navigation icons, such as an up arrow that allows the user to navigate on the top of the app hierarchy.

Below is the code for editing activity_main.xml file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:gravity="center"
app:titleMarginStart="100dp"
app:titleTextColor="@color/black"
android:background="@color/colorPrimary"
app:title="Sms Auto reply App" />
</LinearLayout>
</LinearLayout>

Step 5: Creating BroadcastReceiver class

Create a new Java class extending BroadcastReceiver that implements the onReceive method.

package autoreply.app;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.InputStream;
import java.util.Random;
public class Receive_Sms extends BroadcastReceiver {
private final String SMS="android.provider.Telephony.SMS_RECEIVED";
Context ctx;
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction()==SMS)
{
ctx=context;
Bundle bundle=intent.getExtras();
Object []obj= (Object[]) bundle.get("pdus");
SmsMessage[] smsMessages=new SmsMessage[obj.length];
for(int i=0; i<obj.length; i++)
{
smsMessages[i]=SmsMessage.createFromPdu((byte[])obj[i]);
}
String message=smsMessages[0].getMessageBody();
String phoneNo=smsMessages[0].getOriginatingAddress();
message=message.toLowerCase().toString();
Toast.makeText(context,"Message from : "+smsMessages[0].getOriginatingAddress()+" \n "+smsMessages[0].getMessageBody(),Toast.LENGTH_LONG).show();
try {
Boolean state=true;
MainActivity mainActivity=new MainActivity();
String s=ReadJson(context);
String phoneNo2=mainActivity.phoneNo2;
JSONObject jsonObject=new JSONObject(s);
JSONArray jsonArray=jsonObject.getJSONArray(message);
Random random=new Random();
int rand=random.nextInt(jsonArray.length());
if(rand==jsonArray.length())
{
rand=rand-1;
}
if(phoneNo !=phoneNo2)
{
String answer=jsonArray.getString(rand);
SmsManager smsManager=SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo,null,answer,null,null);
Toast.makeText(context,"Message send to : "+phoneNo,Toast.LENGTH_SHORT).show();
state=false;
mainActivity.phoneNo2=phoneNo;
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, "No possible reply found", Toast.LENGTH_SHORT).show();
}
}
}
private void SendMess(String address,String text)
{
SmsManager smsManager=SmsManager.getDefault();
try
{
smsManager.sendTextMessage(address,null,text,null,null);
}
catch (Exception e)
{
Toast.makeText(ctx, ""+e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
public String ReadJson(Context context)
{
String json="";
try {
InputStream inputStream=context.getAssets().open("Data_info");
int size=inputStream.available();
byte[] buffer=new byte[size];
inputStream.read(buffer);
json=new String(buffer,"UTF-8");
inputStream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return json;
}
}

Step 6: Creating a JSON file

Create a new Android resource directory called assets and create a JSON file inside. Add the following data to the file. The file contains several possible responses to a particular message. You can customize the JSON file to add or update the responses. It will increase its response size and will make the application smarter.

{
"good morning": ["morning too","good morning too","am fine how a you"],
"i love you": ["i love you too","i luv u too","have unconditional love for you dear","i love you dear"],
"love you": ["love you too","more than anything"],
"i miss you": ["I miss you too","i admire to see you","Wow wonderful i miss you more"],
"hi":["Hi too","too","Hi too how a you"],
"how a you": ["Am wonderful","Am fine","am good and you?"],
"hello": ["Hello too","too","Hello how a you?"],
"how was your night": ["Fabulous","Fanatastic","it was cool","i little bit dream","sweet"],
"how was your day": ["Fabulous","Fanatastic","it was cool","i little bit dream","sweet"]
}
view raw Data_info.json hosted with ❤ by GitHub

Step 7: Creating a method in mainactivity.java

Create a method that will request manifest permissions as below.

package autoreply.app;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.os.MessageCompat;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.ContactsContract;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.speech.tts.TextToSpeech;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import giddy.sms_messages_auto_reply.R;
public class MainActivity extends AppCompatActivity {
public static String json11="";
String phoneNo2="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
grantPermissions();
}
protected void grantPermissions()
{
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_SMS,Manifest.permission.SEND_SMS,Manifest.permission.RECEIVE_SMS}, 34);
}
else
{
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode==34 && grantResults[0]==PackageManager.PERMISSION_GRANTED)
{
Toast.makeText(getApplicationContext(), "Permission Granted", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(), "Permission denied", Toast.LENGTH_SHORT).show();
}
}
}

Future Directions

The above implementation is for the autoresponder system for text messages. You can customize the JSON file to expand the range of responses. You can customize your app to read text messages before replying with a text-to-speech class. Automated SMS responses can also be integrated with Artificial Intelligence to build great systems.

Learning Tools

There are plenty of tools that can help in learning Android app development.

  1. Android development documentation: https://developer.android.com/docs
  2. Tutorialspoint tutorials: https://www.tutorialspoint.com/android/index.htm
  3. StackOverflow tutorials: https://stackoverflow.com/questions/15331174/android-developer-first-app-tutorial

Learning Strategy

Before working on this project, the first thing I managed to do was to learn more about the Java programming language and XML. One way to learn Android is to find tutorials online and follow the instructions step by step. Another way is to experiment with features and try different settings to see what effect they have.

Reflective Analysis

I learned a lot while doing this project. I learned about how messages are sent and received on Android devices. I learned about how conditions can be used to test a message from a specific user and to respond accordingly.

You can read more on Android development via this link: https://www.makeuseof.com/tag/send-automatic-replies-text-messages-android/

Conclusion 

This bit-by-bit guide tells you the best way to make and oversee a message auto-reply project in Android Studio. If you think another person could benefit from this aide, share it. Check out the code here.

Hire the author: Zablon M