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 the phone state events.
Create a new project in Android Studio and call it PhoneStateListener
. Use the default Phone & Tablet option and select Empty Activity when prompted for Activity Type.
Although it's not required, you can use the previous recipe to initiate a phone call to view the events. Otherwise, use the default dialer and/or watch the events from an incoming call. (The example code provided in the download files includes the previous recipe to make it easier to view the events.)