I'm currently working on an android application to monitor the incoming and outgoing calls from a phone and register the call info into a file, and from what I've read PhoneStateListener
seems to do what I need.
The thing is I need the application to run on background and I was thinking of using a service for that, but every example I've found that uses the listener declares it in the main activity, so I'm not sure if I need to create a service for it to run on background.
For a little more context, I have specific instructions that I can't create an application to "replace" the default calling app, so there's not much use in creating a GUI (I know the app needs a main activity, but it's only functionality should be starting the monitor).
The idea I have at the moment looks something like:
class CallMonitorService : Service() {
private lateinit var serviceLooper: Looper
private lateinit var serviceHandler: ServiceHandler
private lateinit var monitor: StateMonitor
private inner class ServiceHandler(looper: Looper) : Handler(looper) {
override fun handleMessage(msg: Message) {...}
}
override fun onCreate() {
Log.d(serviceName, "Created")
HandlerThread("ServiceStartArguments", Process.THREAD_PRIORITY_BACKGROUND).apply {
start()
// Get the HandlerThread's Looper and use it for our Handler
serviceLooper = looper
serviceHandler = ServiceHandler(looper)
val manager = getSystemService(TELEPHONY_SERVICE) as TelephonyManager
manager.listen(StateMonitor(), PhoneStateListener.LISTEN_CALL_STATE | ...)
}
}
...
}
class StateMonitor : PhoneStateListener() {
// Handler methods
}
So, as a summary, I need the PhoneStateListener
to be running at all moments, without the need of an app to be running on foreground.
Should I create a service to run the listener on background or the listener runs on background by itself?
Ok, so I've solved the issue.
The short answer is no, the PhoneStateListener
doesn't run in background.
By default the listener runs only when the application is in foreground for what I've seen.
Maybe there's a way to run it in a service but I couldn't get it to work.
Instead I solved the problem with a BroadcastReceiver
.
So, for the solution
The CallStateMonitor
stayed the same as it was before, I just moved it from where it was.
For the service, as I said, I replaced it for a broadcast receiver:
class CallBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val telephonyManager = context?.getSystemService(TELEPHONY_SERVICE) as TelephonyManager
val monitor = StateMonitor()
telephonyManager.listen(monitor, LISTEN_CALL_STATE)
}
private val tag = "STATE_MONITOR"
private inner class StateMonitor : PhoneStateListener() {
override fun onCallStateChanged(state: Int, phoneNumber: String?) {
when (state) {
TelephonyManager.CALL_STATE_IDLE -> Log.d(tag, "IDLE")
TelephonyManager.CALL_STATE_OFFHOOK -> Log.d(tag, "OFF-HOOK")
TelephonyManager.CALL_STATE_RINGING -> Log.d(tag, "RINGING")
}
}
}
}
The rest is just starting the receiver from the main activity:
class CallMonitorActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
registerReceiver(CallBroadcastReceiver(), IntentFilter(ACTION_CONFIGURATION_CHANGED))
Log.d(ACTIVITY_TAG, "Registered call receiver.")
}
}
And that's it, hope it can help someone else ^-^