How to keep a call alive when the app is closed
Why Android stops your call in the background, and how a foreground service with a call notification keeps it running.
On a telehealth app I worked on, users had a strange problem. On some phones, if they closed the app during a video call, the call just stopped.
The fix taught me a lot about how Android treats apps in the background. Here it is, step by step.
Why Android stops your app
Your phone has limited battery and memory. So Android is strict with apps the user can't see:
- Since Android 8, apps in the background are limited in what they can run.
- When memory is low, Android can kill a background app at any time.
- Some phone makers go further and kill background apps even more aggressively.
So if your call lives only inside an Activity, closing the app is a death sentence for it.
The answer: a foreground service
A foreground service is Android's way of saying: "The user knows this is running, so don't kill it." The deal is simple. You must show a notification for as long as it runs. That notification is your promise to the user.
For calls there is a special notification style, CallStyle. It looks like a real phone call notification, with a hang-up button.
class CallService : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val caller = Person.Builder().setName("Dr. Shah").setImportant(true).build()
val notification = NotificationCompat.Builder(this, CALL_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_call)
.setStyle(NotificationCompat.CallStyle.forOngoingCall(caller, hangUpIntent()))
.setContentIntent(openCallScreenIntent()) // tap = back to the call
.setOngoing(true)
.build()
ServiceCompat.startForeground(
this, NOTIFICATION_ID, notification,
ServiceInfo.FOREGROUND_SERVICE_TYPE_PHONE_CALL
)
return START_NOT_STICKY // if the system kills it, don't restart a dead call
}
override fun onBind(intent: Intent?): IBinder? = null
}hangUpIntent() and openCallScreenIntent() are small helpers that return PendingIntents.
Tell Android what kind of service it is
Since Android 14, every foreground service must say what it is for, in the manifest. A call uses the phoneCall type:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_PHONE_CALL" />
<uses-permission android:name="android.permission.MANAGE_OWN_CALLS" />
<service
android:name=".CallService"
android:foregroundServiceType="phoneCall"
android:exported="false" />Start it at the right moment
Since Android 12, you usually can't start a foreground service while your app is in the background. So start it when the call begins, while the user is still looking at the app. Stop it when the call ends:
// When the call connects
ContextCompat.startForegroundService(context, Intent(context, CallService::class.java))
// When the call ends
context.stopService(Intent(context, CallService::class.java))Bring the user back
The last piece is the way back. When the user taps the notification, or opens the app again, they should land on the call screen, not the home screen. The setContentIntent above handles the tap. On app launch, check if a call is active and open the call screen if it is.
The short version
- Android kills apps it thinks nobody needs.
- A foreground service + a visible CallStyle notification tells Android the user needs it.
- Declare the
phoneCalltype and its permissions (Android 14+). - Start it when the call starts, stop it when the call ends.
- Always give the user a way back to the call screen.