How To Build Android SOS Emergency App - Hive Programmers

avatar

Greetings to my favorite science community online, StemSocial.

It's @skyehi and I'm always excited to be back again to continue my series on Android App Development Tutorials for Beginners.

We have achieved quite a lot and have made great progress in this series.

I remember about a month ago when I started this series, we were building simple Apps that displayed toast messages and now we're making much better more complex Apps like Chat Apps, Alarm Clocks, Quiz Apps, Currency Tracker, Periodic Table and even Artificial Intelligence, AI App.

Well guys it's another episode and we're going to be building a very interesting App that I believe is really useful for getting out of sticky or life threatening situations.

There are times where you may find yourself in an unfortunate emergency and you would want to call for help.

Polish_20231218_124523694.pngOriginal Image Source by 200degrees from Pixabay

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

Maybe you happened to be at a place and someone gets a stroke, you would probably call 911 if you're living in America.

One type of App that could be really useful in this situation is an SOS Emergency App.

Some of my readers may be wondering, what exactly is SOS or what does it even stand for.

Well guys "SOS" is basically a Morse code distress signal used purposely for emergencies. It does not actually stand for anything in particular.

It's just a distinctive and easily recognizable sequence of three short signals, three long signals, and three short signals again (··· — — — ···). It is widely understood as a call for help and is used in various communication methods, including the radio to indicate urgent situations or distress.

The term "SOS" has now become synonymous with seeking assistance in times of crisis. So some people actually thought that it was an abbreviation of some phrase but it's really not.

Now that I've gotten the formalities out of the way, let's get started with our work for today

2dk2RRM2dZ8gKjXsrozapsD83FxL3Xbyyi5LFttAhrXxr16mCe4arfLHNDdHCBmaJroMz2VbLrf6Rxy9uPQm7Ts7EnXL4nPiXSE5vJWSfR53VcqDUrQD87CZSpt2RKZcmrYrze8KanjkfyS8XmMCcz4p33NZmfHE4S9oRo3wU2.png

Prerequisites

Of course as always, I'll include this step for the sake of the newcomers to my series. To develop Android Apps on your own computer, you would need to have a couple of softwares installed properly.

  • Android Studio IDE
  • Java Development Kit, JDK

These two are very important softwares for any Android App developer. We build our Apps on Android Studio and we need JDK installed so our computer can execute Java code which is the programming language we'll be using to write the logic or backend code of our App.

As always guys if you're having troubles installing any of these softwares on your PC, please let me know in the comments.

At this point I'll assume you're through with the installation of the softwares. Let's go to the next step where we create a new Android Studio Project for our SOS Emergency App.

Creating Our Android Studio Project

So guys you would want to open your Android Studio IDE and click on "Create a new Android Project". As we usually do, we'll go ahead and select Empty Activity as the template of our App.

Click on next and choose a name for your App. Also decide on the package name of your App and please make sure it's a unique one otherwise if you're trying to publish it on the Play Store it would be rejected.

Finally guys make sure Java is the selected programming language and not Kotlin because we're still using Java for this series.

When you're satisfied with the settings, click on "Finish" and let Android Studio create your App project for you.

2dk2RRM2dZ8gKjXsrozapsD83FxL3Xbyyi5LFttAhrXxr16mCe4arfLHNDdHCBmaJroMz2VbLrf6Rxy9uPQm7Ts7EnXL4nPiXSE5vJWSfR53VcqDUrQD87CZSpt2RKZcmrYrze8KanjkfyS8XmMCcz4p33NZmfHE4S9oRo3wU2.png

Designing the User Interface, UI

Well guys, I love working on designing App's user interface a lot but an SOS emergency App doesn't really focus on how cool the App UI is.

The most important thing is that one button that triggers a call or a message that gets the the help you need.

This is why the only element we'll need for our App is one button that does the job. More of the work will be done in the backend code of our App.

However, if you want to add more features to your App, it may require more UI elements the user will interact with.

The user interface design of our App will be made inside our activity_main.xml file. So we're just going to design a simple UI with a panic button.

Here's how your code should look like

<Button
    android:id="@+id/panicButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Panic Button" />

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

Implement Panic Button Functionality

We're now going to start working on our backend. There are three main things we'll be doing for the backend.

We'll first write the code that handles the Button's click event, then we'll request permission and finally create a code to send an emergency SMS. Pretty easy and simple steps right guys.

Alright so in your MainActivity.java file, let's handle the panic button click event using the setOnClickListener method.

Here's how your code should look like.

import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

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

        Button panicButton = findViewById(R.id.panicButton);

        panicButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendEmergencyAlert();
            }
        });
    }

    private void sendEmergencyAlert() {
        // Implement emergency alert functionality
        // ...

        // Example: Display a toast message
        Toast.makeText(this, "Emergency alert sent!", Toast.LENGTH_SHORT).show();
    }
}

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

Requesting Permissions and Accessing Location Services

Now there are two main services that any SOS emergency App would definitely need. One is the location service that will tell the people you're sending the emergency to, where you currently are and another service would be the SMS service.

Of course if you intend to build an SOS App that uses the internet you'll probably need to add internet permission but I'll recommend SMS service because maybe the individual going through the emergency may not have Internet connection or the people he or she is trying to contact may be offline as well.

So guys we'll add the necessary permissions in the AndroidManifest file and then afterwards we'll go back to our MainActivity.java file to implement location services which would help us obtain the user's current location.

Here's how your permission code should look

(html comment removed:  Add permissions )
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.SEND_SMS" />

Now guys let's also work on implementing the location service

// Inside your MainActivity.java
private void sendEmergencyAlert() {
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    double latitude = location.getLatitude();
    double longitude = location.getLongitude();

    String message = "Emergency! Help needed at " + latitude + ", " + longitude;
    sendSMS("emergencyContactNumber", message);
}

YpihifdXP4WNbGMdjw7e3DuhJWBvCw4SfuLZsrnJYHEpsqZFkiGGNCQTayu6EytKdg7zA3LL2PbZxrJGpWk6ZoZvBcJrADNFmaFEHagho8WsASbAA8jrpnELSvRtvjVuMiU1C5ADFX1vJgcpDvNtue9Pq83tjBKX62dqT5UoxtDk.png

Sending Emergency Alert

For our final step, we would need to create a method that will enable our SOS Emergency App to send the emergency alert, whether through SMS, push notification, or API call.

So guys if it's a call, you would probably need to implement call functionality like we did in our previous blog on creating a Phonecall App.

However if you're interested in seeing how adding phonecall functionality would look like, just let me know in the comments.

Here's how your code should look like

private void sendSMS(String phoneNumber, String message) {
    SmsManager smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage(phoneNumber, null, message, null, null);
}

2dk2RRM2dZ8gKjXsrozapsD83FxL3Xbyyi5LFttAhrXxr16mCe4arfLHNDdHCBmaJroMz2VbLrf6Rxy9uPQm7Ts7EnXL4nPiXSE5vJWSfR53VcqDUrQD87CZSpt2RKZcmrYrze8KanjkfyS8XmMCcz4p33NZmfHE4S9oRo3wU2.png

Running Our SOS Emergency App

Congratulations guys, I hope you're excited about this new project. We've been able to complete a simple SOS Emergency App. So if you're in some kind of distressing situation you can use your own App to call for help.

Of course in future blogs we're be building a much better, more complex version of the App using Kotlin.

It's time to run the app. You can run it on an emulator or a physical Android device. Now when your App finally launches, press the panic button to simulate an emergency and check to see that the alert is triggered. Everything should work just fine with the codes I've given.

Thanks so much for taking the time to read today's blog. I hope you enjoyed this particular tutorial. It gives me so much joy knowing how far we have come with this series.

As you keep improving yourself, you'll soon become a professional Android App Developer. Have fun out there guys.

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


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



0
0
0.000
1 comments
avatar

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. 
 

0
0
0.000