Building Apps using the Android Telephony API

The Android Telephony API allows developers to access and control telephony features on Android devices.

Building Apps using the Android Telephony API
Image by Guido Coppa / Unsplash

Imagine you have a reliable assistant who can record and store important voice memos for you. Your assistant, in this case, is the call recording app you're building, and the Android Telephony API acts as the assistant's advanced voice recording capabilities.

Just like your assistant, the app is designed to be user-friendly. You create a simple and intuitive user interface that allows users to easily start and stop recording calls, just as you would instruct your assistant to start and stop recording a voice memo.

Similar to your assistant needing permission to access your conversations, the app requires the necessary permissions. You declare these permissions in the app's manifest file and request runtime permissions from the user, ensuring that the app has the necessary access to record calls.

Without further ado, let's get started!

Android Telephony API

The Android Telephony API allows developers to access and control telephony features on Android devices. It provides a set of classes and methods that enable developers to interact with the phone's telephony functionality, including making and receiving calls, managing call states, and accessing call-related information.

Think, for instance, of the call recording functionality of any application as your assistant's ability to listen to and record your voice memos. Using the Android Telephony API, the app likewise detects incoming and outgoing calls and starts recording the audio. It captures the conversation, just as your assistant would capture your voice memos at your command.

The Android multimedia framework extends support for capturing and encoding various audio and video formats. If your device hardware supports it, you can employ MediaRecorder APIs.

— Cross Platform Development

With a plethora of developer resources, you can easily create Android applications that leverage telephony features, such as call recording apps, call management apps, and caller ID apps. Popular applications like TrueCaller, Showcaller, and CallApp leverage the native Android Telephony API to enhance the user experience by providing additional functionality like Caller ID, blocking spam calls, and customization options.

On the other hand, Apple offers the Core Telephony framework for telephony functionality. This framework provides a set of classes and protocols that allow developers to access and manipulate telephony-related information on iOS devices. It enables developers to retrieve information about the device's cellular network, such as the carrier name, signal strength, and current call status.

One popular app on the App Store that takes advantage of the Core Telephony framework is iCall Recorder on the App Store. This app leverages the framework's capabilities to access and manipulate telephony-related information on iOS devices. With Call Recorder iCall, users can retrieve information about their device's cellular network, including the carrier name, signal strength, and current call status. This app allows users to record their phone calls, providing a convenient and efficient way to keep track of important conversations.

While there is a continental divide between two major operating systems, Android and iOS, cross-platform development is not just an option for big companies, but a serious endeavor. That's part of why we are witnessing an explosive trend in low-code and no-code tools like Softr, AppSmith, Bubble, and Glide allowing anyone, regardless of their hardware sophistication, to rapidly build and deploy cross-platform applications.

— Using Android Telephony API

To use the Android Telephony API, developers need to request the necessary permissions in the app's manifest file. These permissions include READ_PHONE_STATE, which allows access to the phone's state and information, and RECORD_AUDIO, which enables recording of phone calls.

Once the necessary permissions are granted, developers can use the TelephonyManager class to access telephony-related information, such as the device's phone number, network operator, and call state. They can also use the CallManager class to manage calls, including making outgoing calls, answering incoming calls, and ending ongoing calls.

For call recording apps, the Android Telephony API provides the ability to capture audio from phone calls. Developers can use the MediaRecorder class to record the audio stream during a call and save it to a file. They can also use the AudioManager class to control the audio settings during a call, such as the volume and speakerphone mode.

Remember, however, it's important to respect privacy and legal regulations when using the app. As we will highlight the key factors for building a simple call-recording mobile app, we will also provide a general outline to help you inform end-users about the recording feature and comply with the laws regarding call recording in your country to ensure a responsible and ethical use of the app.

Key Factors for Building Apps using the Telephony Functionalities

Let's consider the key factors for leveraging MediaRecorder to develop an android application that captures audio from the device microphone, saves the audio, and allows for playback using MediaPlayer. We will explore the basic outline for building a call recorder using Android services.

— Environment Setup

First off, verify that you can run an Android app with Compose support. To do this, ensure that you have the latest version of Android Studio Arctic Fox (version 2020.3.1 Patch 1) or a newer distribution. By doing so, you'll be able to take advantage of the intelligent editor features and the compose preview.

— Project Details

When you create a new project with Compose support, make sure to follow these steps:

  • Open the New Project window.
  • Select "Empty Compose Activity".
  • Fill out the standard fields (application name, package name).
  • Set the minimum SDK to API 21 (Lollipop).
  • Click Finish. For example, you can name your project "Recordio" because it has a cool sound and includes the word "record".

— Setting the Java Version

When creating new projects with Gradle 7 as the build system, it is important to have Java 11 installed. Follow this checklist to ensure that your project uses this version:

  • In your application-level build.gradle (Located at app/build.gradle), make sure that sourceCompatibility and targetCompatibility are set to JavaVersion.VERSION_11 (located under android -> compileOptions). You can use a different compatible version if you have it installed (e.g., JavaVersion.VERSION_12).
  • Set a compatible Gradle JDK under Gradle Settings, which can be accessed from the Project Structure window under SDK Location. After Android Studio completes building the project, we are introduced to MainActivity.kt, the main entry point for our application.

— Jetpack Compose

Jetpack Compose, recently launched as a stable 1.0 version, is now production-ready after exiting the beta phase. According to Google, it offers a modern toolkit for creating native Android UI with simpler development, powerful tools, and intuitive Kotlin APIs.

It is worth noting that Jetpack Compose exclusively supports Kotlin due to its extensive utilization of Kotlin's coroutines. If you are unfamiliar with coroutines, the official guide is a great resource to begin with.

— A Basic UI

Traditionally, Android uses activities and fragments to separate screens and independent flows. However, with Compose, applications follow a single-activity architecture, making it easier to introduce complex navigation flows using Compose Navigation.

All navigation logic is handled by the Compose runtime, which means that the standard activity and fragment lifecycles do not apply here. Instead, Compose defines different side effects that can act as lifecycle callbacks.

— Recognizing calls in Android

In Android, applications often need to interact with various components of the environment. These components can include system interfaces like the device's networking interface or other user applications that are currently installed. To facilitate this, the Android SDK provides a convenient form of the publisher-subscriber pattern called Broadcasts.

Implementing a Broadcast Receiver, which is an architectural component, enables an app to broadcast receives. The receiver implements a single method called onReceive, which is invoked with a Context and an Intent representing the desired broadcast. An intent filter is used to specify the events to which a receiver is bound. In our case, we will implement the CallBroadcastReceiver to handle both incoming and outgoing calls. Here's the declaration in the manifest:

— Persisting calls

Now that we have the capability to identify incoming and outgoing calls, we want to save them in a database. There are generally two options for preserving data:

Using cloud storage can be expensive and requires user authentication within our application. Despite this, online databases offer persistence across devices and installations of the application.

On the other hand, local storage is easy and cost-effective, but it only retains information on the specific device where the application is installed.

End note

These are the general development steps you need to go through in order to create a call recording application. This is not an easy process and quite voluminous, but you already have the general structure.

Banner Image by Matt Wojtaś on Unsplash