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 the phone state events.

Getting ready

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. ...

Get Android Application Development Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.