Reputation: 304
I am developing an Android app that needs to detect incoming and outgoing phone calls, even when the app is killed. I have implemented a BroadcastReceiver
(CallReceiver
) to listen for TelephonyManager.ACTION_PHONE_STATE_CHANGED
, and it works fine when the app is running in the background.
However, the issue occurs when the app is force-stopped or killedโthe BroadcastReceiver
no longer receives phone call events. I want to detect phone calls without relying on a Foreground Service with a persistent notification.
Using a BroadcastReceiver
CallReceiver
to listen for TelephonyManager.ACTION_PHONE_STATE_CHANGED
.Using WorkManager
Worker
that registers the CallReceiver
.WorkManager
is not designed for real-time broadcast listening.Using a Foreground Service (Works, but not ideal)
CallMonitoringService
that registers the receiver dynamically.Using a Bound Service
Bound Service
stops when the app is killed, so it does not solve the problem.class CallReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (context == null || intent?.action != TelephonyManager.ACTION_PHONE_STATE_CHANGED) return
val state = intent.getStringExtra(TelephonyManager.EXTRA_STATE)
val incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER)
when (state) {
TelephonyManager.EXTRA_STATE_RINGING -> {
Log.d("CallReceiver", "Incoming call from: $incomingNumber")
}
TelephonyManager.EXTRA_STATE_OFFHOOK -> {
Log.d("CallReceiver", "Call answered or dialed!")
}
TelephonyManager.EXTRA_STATE_IDLE -> {
Log.d("CallReceiver", "Call ended!")
}
}
}
}
CallReceiver
when the app is killed?JobScheduler
or BroadcastReceiver
be used differently to achieve this?Any suggestions or workarounds would be greatly appreciated! ๐
Upvotes: 0
Views: 43