Sonde Health API Platform Documentation
Developer guide - Background, Call handling, and Notifications
This is a guide for developers to understand how Sonde handles Background, Call handling and related Notifications. This will help them to integrate quickly into their existing app.
Below are different flows the demo app currently works:
App running in the background:
We have achieved the background handling using the foreground service.
To protect the user privacy concerns, we have displayed a persistent notification, which will inform the user about the ongoing voice analysis service. (Refer to the code updated below).
The voice analysis will continue running in the background and can generate the scores in the background.(Refer to the code updated below).
The User can observe the progress of voice analysis from the notification.
Refer the below code snippet for the background handling:
Declaring the system-level permission and service
Need to update AndroidManifest.xml for permission and service
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE_MICROPHONE" />
Â
<service android:name=".service.VoiceAnalysisService" android:enabled="true" android:foregroundServiceType="microphone" android:stopWithTask="true" />
Define the foreground service
Refer to the code in VoiceAnalysisService.kt
Create the foreground service and bind it to the fragment/activity to update the UI.
private val binder: IBinder = VoiceAnalysisBinder()
inner class VoiceAnalysisBinder : Binder() { val service: VoiceAnalysisService = this@VoiceAnalysisService } override fun onBind(intent: Intent?): IBinder { return binder }
Create a notification channel & start the foreground service, on receiving the
StartService
Receive the events from Activity/Fragment.
Based on the above event, we have to start or restart the service by calling
startContinuousVoiceAnalysis
& to stop the service callstopContinuousVoiceAnalysis
.To send updates back to fragment we have used the
StateFlow
.
Fragment changes to update the UI and handle the service states
Refer the code in VerificationFragment.kt
Binding the service in onViewCreated
Create service connection in the fragment.
Once the service is connected, you can start communication with the service by sending the events.
Perform the required actions based on the state change of service which can be listened to using flow inside the onServiceConnected of the voiceAnalysisServiceConnection.
voiceAnalysisService.serviceState.collectLatest
{state ->
}
Phone call handling
Refer the code in VerificationFragment.kt
Incoming phone call handling is done using a broadcast receiver with
TelephonyManager.EXTRA_STATE
.Register the receiver in the
onViewCreated
.Unregistered the receiver in
onDestroy
this.activity?.unregisterReceiver(receiver)
Initialize the receiver
Upon getting the trigger we can send the event to the service as per requirements.
Expected app behaviour
Incoming call while the app is in the foreground:
When the app is running in the foreground, and the user receives an incoming call, the voice analysis will stop.
After the call ends, the voice analysis will automatically resume.
Incoming call while the app is in the background:
If the app is running in the background and the user receives an incoming call, the voice analysis will stop.
After the call ends, the app will send a notification stating "Click here to start the analysis."
Upon clicking the notification, the app will launch, and the voice analysis will automatically restart.
Â
Â
Sonde Health