📱 How to capture start and end times of a phone call and calculate total call duration

:iphone: How to capture start and end times of a phone call and calculate total call duration.

The example app phoneCallDuration is a template for a more sophisticated app to log phone calls using Blocks. The provided code captures:

  • the time of start ringing of a phone call,
  • time of termination of a phone call,
  • duration of initiated and received phone calls and
  • the phone number called or calling you.

Incoming Call Duration and Outgoing Call Duration simulation

Capturing Total Incoming Call Duration

Why the template may be useful
You can modify the existing code to make a phone logger using the available information.

The template shows how to use the PhoneCall.IncomingCallAnswered, PhoneCallStarted and PhoneCallEnded Blocks to capture the start and ending times of a phone conversation. It uses the Clock to calculate the call duration.

A simple phone calling scheme is provided (enter a number in a TextBox, then use the Call Out button.).

This app template does not run in the background. You might

since the template app will not capture the call duration if the phone screen goes to sleep. Discussions in the forum show how you can possibly use the app to stay alive and not sleep.

The duration calculation uses a DurationToSeconds and a DurationToMinutes Block to display call time duration ( 05:12 minutes:seconds ). Be aware this is the total calling time including the ringing time before the call is actually answered (because that is what the provided Blocks capture).

Button2 is provided to check the duration algorithm. The Button2 is set to visible = false unless you want to set the visibility to true to test.

The app was tested on an Android 13 Samsung 13 cell phone using Companion 2.66u and nb192a. It uses the MakePhoneCallDirect instead of the MakePhoneCall Method, consequently the app as is may not be accepted by the PlayStore. If that is an issue, use the MakePhoneCall method instead.

For you to do with the template:
You can

  • use a TinyDB to capture an incoming and outgoing phone log.
  • make the app more user friendly and create a more attractive interface.
  • do what you want to do with the phone information in the app you build.
  • rebuild the app as a Foreground or Background Service.
  • refactor the code to do exactly what you require.

Reality Check
The Phone tools all capturing total time of ringing plus conversation because the actual conversation time cannot be determined using the Phone Blocks. Be happy with what is possible or create an extension to do exactly what you need to determine.

Event timing that is possible to capture using Blocks:

Event indicating that a phone call has ended. The status can be any of:
1: Incoming call was missed or rejected
2: Incoming call was answered and hung up
3: Outgoing call was hung up.

Event indicating that a phone call has started. The status can be any of:
1: Incoming call is ringing
2: Outgoing call is dialed

phoneCallAnswer3.aia (10.1 KB)

I hope developers find these code snippets useful. Suggestions (and code) for improvement (show code for using the concept for a Foreground or Background app) ?