VOOZH about

URL: https://www.geeksforgeeks.org/android/how-to-build-a-sos-mobile-application-in-android-studio/

⇱ How to Build a SOS Mobile Application in Android Studio? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Build a SOS Mobile Application in Android Studio?

Last Updated : 30 May, 2026

An SOS mobile application is an emergency assistance app designed to help users quickly contact trusted people during dangerous or critical situations. These apps provide fast communication and safety features that can be triggered without manually operating the phone for a long time.

  • Sends emergency alerts and live location to saved contacts instantly
  • Provides quick access to help during accidents, attacks, or medical emergencies
  • Uses accessibility features like shake detection or emergency buttons for faster response

Pre-requisites:

Step by Step Implementation

Follow these steps to create a Mobile Application in Androide Studio.

Step 1: Create a New Project

  • Open Android Studio
  • Click New Project
  • Select Empty Activity
  • Language: Java or Kotlin
  • Minimum SDK: Android 6.0 or above
  • Finish project setup

Step 2: Creating the Contacts Module

Create a Contacts folder to manage all contact-related files used for storing and displaying emergency contacts in the ListView.

Step 2.1: Creating model class for Contact

We create this class to store contact details like name and phone number in a structured format. It also ensures the phone number is properly validated before use.

 Step 2.2: Creating a Database Helper class

We use this class to store and manage emergency contacts using SQLite database. It ensures contacts remain saved permanently even after app restart.

Step 2.3: Creating a CustomAdapter.java

We create this to connect contact data with ListView for proper display. It also handles actions like showing and deleting contacts from the list.

Step 2.4: item_user.xml

We design this layout to define how each contact will look in the ListView. It improves UI by showing name and phone number in a structured format.

Step 3: Creating the Service Module

We create this module to handle background tasks like shake detection and SOS alerts. It ensures the app works even when it is not open.

Step 3.1: Creating ShakeDetector class

We use this to detect device shaking using accelerometer sensor. It triggers SOS only when a specific shake pattern is detected.

 Step 3.2: Creating the SensorService

We create this to run continuous background monitoring of shake events. It sends emergency SMS with location even if the app is closed.

 If you start a service starts with the START STICKY return type, it will run in the background even if the host activity is not running in the foreground. If Android has to forcibly close a program due to a memory error or other reasons, the service will be restarted without the user's intervention.

In order to make the user aware that the Shake event has been registered or say the messages have been delivered, we create a vibrate() method. This will make the phone vibrate in a defined wave format.

 We use FusedLocationProviderClient to get the user’s current location using getCurrentLocation(). It requires GPS/location services to be ON, otherwise it returns null. From Android O onwards, location access only works when the user explicitly enables location for privacy and awareness.

After getting the location successfully, we use SmsManager to send emergency messages to all saved contacts from the database. If location is not available, we send a fallback message without coordinates so receivers still get alert and can contact help or authorities for assistance.

Till now whatever we have done will work until the activity is in Foreground or Running. But what when the user kills the application? Or locks the phone? For this, we create a BroadcastReceiver.

Step 3.3: Creating the Broadcast Receiver

  • Whenever a service is destroyed, the onDestroy method is called, and we will use that method to call a broadcast receiver before the service is actually destroyed. 
  • The broadcast receiver in return again starts the service. And our problem is resolved!! The service now runs without the host activity in the background.e.

Step 4: Working with the MainActivity

Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 

Step 5: Working with AndroidManifest.xml

Steps To Run SOS App in Android Studio (Final Execution Steps)

Step 1: Open Project

  • Open Android Studio
  • Click File -> Open
  • Select your SOS project folder and Wait for Gradle Sync to complete

Step 2: Build Project

  • Click Build -> Make Project
  • Ensure no errors appear in bottom window If error comes, first resolve dependencies

Step 3: Connect Device / Emulator

You can run app in 2 ways:

1. Emulator

  • Open Device Manager
  • Start virtual device

2. Real Device (Recommended)

  • Enable Developer Options
  • Turn ON USB Debugging
  • Connect mobile via USB cable

Step 4: Install & Run App

  • Click Run button (Green Play icon)
  • Select device and Wait for installation

Step 5: First Launch Setup (Very Important)

When app opens first time:

Allow all permissions:

  • Location -> “Allow all the time”
  • SMS permission -> Allow
  • Contacts -> Allow

Go to Settings if asked:

  • Disable Battery Optimization for this app
  • Enable Background Location

Output:

Future Scope

  • We can add options for personal assistance emergencies like a map that indicates nearby police stations, hospitals, cabs, etc.
  • We create a logic that sends the user location every 1 or 2 minutes without the user shaking the device again.
  • We can add voice or/and video recording functionality.
  • Also, you can add a call to multiple or a single person at the time of shake.
  • We can try to add OpenCellId, this will allow you to fetch the location of the nearest mobile tower. But again remember that in order to calculate the location you would require some device information(no GPS) and that would require your GPS or location services to be turned ON.
  • Doors to changes in the UI are always open.

Notes:

1. Allow the app to autostart, in order to use the app while the screen is off.

2. Remove any battery optimization constraints on the app. This might make Android kill the service.

3. Allow all the permissions, especially allow location permissions by Allowing the app to use the device location all the time. This would allow the service to use the device location when the shake event is registered.

Comment
Article Tags:

Explore