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?
from Does android PhoneStateListener run on background?
No comments:
Post a Comment