Monitoring phone call events
In the previous recipe, we demonstrated how to make a phone call, both with an Intent to call the default application, as well as by directly dialing the number with no UI.
What if you want to be notified when the calls ends? This is where it gets a bit more complicated, as you'll need to monitor the Telephony events and track the phone state. In this recipe, we'll demonstrate how to create a PhoneStateListener
to read phone state events.
Getting ready
Create a new project in Android Studio and call it PhoneStateListener
. Use the default Phone & Tablet options
and select Empty Activity
in the Add an Activity
to Mobile
dialog.
Although it's not required, you can use the previous recipe to initiate a phone call. Otherwise, use the default dialer and/or watch the events from an incoming call.
How to do it...
We only need a single TextView
on the layout to display the event information. Open the activity_main.xml
file and follow these steps:
- Add or modify the
TextView...