Monitoring Changes in Phone State Phone Activity and Data Connections

The Android telephony API lets you monitor phone state, retrieve incoming phone numbers, and observe changes to data connections, signal strength, and network connectivity.

In order to monitor and manage phone state, your application must specify the READ_PHONE_STATE uses-permission in its manifest using the following XML code snippet:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

Changes to the phone state are monitored using the PhoneStateListener class. Extend the Phone State Listener to listen for, and respond to, phone state change events including call state (ringing, off hook, etc.), cell location changes, voice-mail and call-forwarding status, phone service changes, and changes in mobile signal strength.

To react to phone state change events, create a new Phone State Listener implementation, and override the event handlers of the events you want to react to. Each handler receives parameters that indicate the new phone state, such as the current cell location, call state, or signal strength.

Listing 12-7 highlights the available state change handlers in a skeleton Phone State Listener implementation.

LISTING 12-7: Phone State Listener skeleton class

Available for download on Wrox.com

PhoneStateListener phoneStateListener = new PhoneStateListener() { public void onCallForwardingIndicatorChanged(boolean cfi) {} public void onCallStateChanged(int state, String incomingNumber) {} public void onCellLocationChanged(CellLocation location) {} public void onDataActivity(int direction) {} public void onDataConnectionStateChanged(int state) {} public void onMessageWaitingIndicatorChanged(boolean mwi) {} public void onServiceStateChanged(ServiceState serviceState) {} public void onSignalStrengthChanged(int asu) {}

Once you've created your own Phone State Listener, register it with the Telephony Manager using a bitmask to indicate the events you want to listen for, as shown in Listing 12-8.

LISTING 12-8: Registering a Phone State Listener

Available for telephonyManager.listen(phoneStateListener, download on PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR |

Wrox.com continues

LISTING 12-8 (continued)

PhoneStateListener.LISTEN_CALL_STATE | PhoneStateListener.LISTEN_CELL_LOCATION | PhoneStateListener.LISTEN_DATA_ACTIVITY | PhoneStateListener.LISTEN_DATA_CONNECTION_STATE | PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR | PhoneStateListener.LISTEN_SERVICE_STATE | PhoneStateListener.LISTEN_SIGNAL_STRENGTH);

To unregister a listener, call listen and pass in PhoneStateListener.LISTEN_NONE as the bit field parameter, as shown below:

telephonyManager.listen(phoneStateListener,

PhoneStateListener.LISTEN_NONE);

Was this article helpful?

+1 0
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