How To Build An Android SMS Application - Hive Programmers

in StemSocial2 years ago

Greetings to my favorite science community online, StemSocial.

It's @skyehi and I'm super excited to be back to continue my series on Android App development tutorials for beginners. It's been quite a while since I started this series and I'm very happy with all the feedback I'm getting from my readers.

Today's blog is a really special one because we're going to be starting our journey to building social media platforms. Social Media Apps are amongst the most used apps on the Smart phone.

Everyone is always trying to communicate with friends and family with their phone, making social media platforms and messengers the most important tools on our smart phones

For today's tutorial, I'm going to teach you how to build a basic SMS messenger app. We have all received SMS and sent SMS before. Your internet service provider and even banks mostly communicate with you as a customer through these SMS Apps.

Let's learn some new programming concepts as we attempt to build a basic SMS app.

Polish_20231124_160059686.jpgOriginal Image Source by Pexels from Pixabay

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

Our attempt to build an Android SMS App would involve several steps, which would include the User Interface UI design, App permissions, SMS handling, and a few other things I'll be talking about in this blog.

This tutorial is just to cover the basics because it's a programming tutorial for beginners.

2dk2RRM2dZ8gKjXsrozapsD83FxL3Xbyyi5LFttAhrXxr16mCe4arfLHNDdHCBmaJroMz2VbLrf6Rxy9uPQm7Ts7EnXL4nPiXSE5vJWSfR53VcqDUrQD87CZSpt2RKZcmrYrze8KanjkfyS8XmMCcz4p33NZmfHE4S9oRo3wU2.png

Setting Up Our App Project

Alright guys, at this point we're used to the first step. We need to set up our App project on Android Studio. You would need to ensure that Android Studio is installed on your computer because that's the platform we'll be using to write our code and build the SMS App.

I'll assume my readers have successfully installed Android Studio and also the Java Development Kit JDK, since we'll be using Java as our programming language and your computer would need the JDK in order to be able to execute Java code.

So now, open Android Studio and click on "create a new project" . You can set the project name and choose an appropriate location. Choose the "Empty Activity" template to make everything simple.

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

Designing Our App User Interface

Since we're all set with creating the Project, it's time to work on the user interface of our SMS. This is the frontend or the part of the App that our users will see and interact with.

Our SMs App User Interface layout will be designed in the activity_main.xml file.

It wouldn't be too complex a design. We'll be adding an EditText for entering the phone number of the person you want to text, another EditText for composing the message, and a Button to send the SMS.

Here's how your code should look like

<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"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editTextPhoneNumber"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Phone Number"/>

    <EditText
        android:id="@+id/editTextMessage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/editTextPhoneNumber"
        android:hint="Message"/>

    <Button
        android:id="@+id/buttonSend"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/editTextMessage"
        android:text="Send SMS"/>
</RelativeLayout>

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

Requesting SMS Permissions

Like I mentioned earlier, we would need to get some permissions. For our SMS App, the permission we need to request is an SMS permission so that our App will be able to access SMS functionality on the Android Device.

Without this step, the App may not be able to send an SMS or it may even crash when you try to run it on your Android Device or Emulator.

If you've been following this series, I believe you already know where the Permission code will be written.

We'll be requesting SMS permission in the AndroidManifest.xml file,

Here's how your code should look like.

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

Now another permission is required but this time it would be written inside your MainActivity.java file and obviously with Java Programming language. What we're doing is requesting runtime permissions.

Here's how your code should look like.

private static final int PERMISSION_REQUEST_SMS = 1;

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

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS},
                PERMISSION_REQUEST_SMS);
    }
}

After writing that code we also need to add another set of codes that would handle the permission request result. This step is very necessary if you want your SMS App to function effectively without any error or without the App crashing.

Here's how the code to handle the permission request results should look like

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (requestCode == PERMISSION_REQUEST_SMS) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // Permission granted, you can proceed with sending SMS
        } else {
            // Permission denied, handle accordingly
        }
    }
}

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

The Code For Sending an SMS

Now guys, it's time for us to write the code for the main functionality. The above codes we wrote gives our App Permission to send the SMS but the code we're about to write is the actual SMS function.

In order for our App to be able to send SMS we need to implement the SMS sending functionality in the MainActivity.javafile. Trust me guys it's a very simple step.

Here's how your code should look like in MainActivity.java file

Button sendButton = findViewById(R.id.buttonSend);
sendButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        EditText phoneNumberEditText = findViewById(R.id.editTextPhoneNumber);
        EditText messageEditText = findViewById(R.id.editTextMessage);

        String phoneNumber = phoneNumberEditText.getText().toString();
        String message = messageEditText.getText().toString();

        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(phoneNumber, null, message, null, null);
    }
});

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

Running Our SMS App

That's just it guys, we're done building a basic SMS App that can send an SMS message to the phone number you insert in.

It's time to run the App guys. You can connect your Android device or use an emulator, then run your app.

When the SMS App opens, enter a phone number, type a message, and click the "Send SMS" button to test your SMS App. It should work just fine with the codes we wrote.

2dk2RRM2dZ8gKjXsrozapsD83FxL3Xbyyi5LFttAhrXxr16mCe4arfLHNDdHCBmaJroMz2VbLrf6Rxy9uPQm7Ts7EnXL4nPiXSE5vJWSfR53VcqDUrQD87CZSpt2RKZcmrYrze8KanjkfyS8XmMCcz4p33NZmfHE4S9oRo3wU2.png

Thank you so much for taking the time to read today's blog. I hope you enjoyed this tutorial. As always, if you're having trouble with either installing the necessary softwares, writing the code or running the App, please let me know in the comments section and I'll try to be of assistance.

Have a lovely day and catch you next time on StemSocial. Goodbye 👨‍💻


You Can Follow Me @skyehi For More Like This And Others

Sort:  

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

Thanks for including @stemsocial as a beneficiary, which gives you stronger support. 
 

Thats usefull

Picture your business as a car zooming down the highway of communication, and SMSLive is your smooth, fast lane. Whether you’re sending a quick marketing nudge or a full-blown campaign, this platform is your highway patrol, keeping everything running smoothly. With its real-time delivery tracking, you’re in control, able to see exactly where your messages are going and when they arrive. No more getting stuck in traffic – the bulk SMS feature means you can send thousands of messages in one go, seamlessly and efficiently. The custom sender ID is like your personalized license plate, giving every message a recognizable flair. And let’s not forget about the global reach – SMSLive is like driving on a worldwide freeway, connecting you with customers from Sydney to Seattle. So buckle up and put your communication into high gear with SMSLive https://smslive.pro. It’s the open road your business has been waiting for.