Monitoring Incoming Phone Calls

One of the most popular reasons for monitoring phone state is to detect, and react to, incoming phone calls.

To do so, override the onCallStateChanged method in a Phone State Listener implementation, and register it as shown in Listing 12-9 to receive notifications when the call state changes.

LISTING 12-9: Monitoring phone calls

Available for PhoneStateListener callStateListener = new PhoneStateListener() { Wrox°comn public void onCallStateChanged(int state, String incomingNumber) { // TODO React to incoming call.

telephonyManager.listen(callStateListener,

PhoneStateListener.LISTEN_CALL_STATE);

The onCallStateChanged handler receives the phone number associated with incoming calls, and the state parameter represents the current call state as one of the following three values:

> TelephonyManager.CALL_STATE_IDLE When the phone is neither ringing nor in a call

> TelephonyManager.CALL_STATE_RINGING When the phone is ringing

> TelephonyManager.CALL_STATE_OFFHOOK When the phone is currently in a call

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


Post a comment