Android PhoneStateListener Example


Android PhoneStateListener

While searching through some documentation I found an interesting class of android through which we can listen to some basic network related events, called Android PhoneStateListener which lead to this tutorial Android PhoneStateListener Example. Another class which needs an introduction is TelephonyManager, as this is the class which uses the Android PhoneStateListener to listen telephony service updates. This tutorial is the first one in the series of exploring Android PhoneStateListener and TelephonyManager classes at Truiton.com. Links to complete series are:

1) Android PhoneStateListener Example
2) Android onDataConnectionStateChanged – Detect Network Type
3) Android onSignalStrengthsChanged – LTE Strength Measurement

To start off let me give a brief intro about what all things I’ll be covering in this tutorial. Here I would be instantiating an object of Android TelephonyManager and listening to the updates of Context.TELEPHONY_SERVICE. To monitor the changes of specific telephony states like PhoneStateListener.LISTEN_CELL_INFO, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE and many more, I will be declaring a class CustomPhoneStateListener which will extend Android PhoneStateListener and override all of its methods.

Android TelephonyManager

This class of android is the main class through which one can access the telephony services of a device. All the information in this class is accessed through a system-level service called Context.TELEPHONY_SERVICE. Since this tutorial is focused on Android PhoneStateListener lets not get in depth of this class. The only relevant method for us in this class is TelephonyManager.listen(PhoneStateListener listener, int events). And for this method I will be declaring a custom PhoneStateListener.

Android PhoneStateListener

This is the actual class which gets callbacks, whenever some change is observed in TELEPHONY_SERVICE. Android PhoneStateListener has many callback methods which are called if we register for their specific LISTEN_ flag in the TelephonyManager’s listen method. Have a look at the listing below to correlate LISTEN_ flags with Android PhoneStateListener’s callback methods:

Android PhoneStateListener callback methods
LISTEN_ Flag Callback Method
PhoneStateListener.
LISTEN_CALL_STATE
onCallStateChanged(int state, String incomingNumber)
PhoneStateListener.
LISTEN_CELL_INFO
onCellInfoChanged(List<CellInfo> cellInfo)
PhoneStateListener.
LISTEN_CELL_LOCATION
onCellLocationChanged(CellLocation location)
PhoneStateListener.
LISTEN_DATA_ACTIVITY
onDataActivity(int direction)
PhoneStateListener.
LISTEN_DATA_CONNECTION_STATE
onDataConnectionStateChanged(int state, int networkType)
PhoneStateListener.
LISTEN_SERVICE_STATE
onServiceStateChanged(ServiceState serviceState)
PhoneStateListener.
LISTEN_SIGNAL_STRENGTHS
onSignalStrengthsChanged(SignalStrength signalStrength)
PhoneStateListener.
LISTEN_CALL_FORWARDING_INDICATOR
onCallForwardingIndicatorChanged(boolean cfi)
PhoneStateListener.
LISTEN_MESSAGE_WAITING_INDICATOR
onMessageWaitingIndicatorChanged(boolean mwi)

Now since we are dealing here with phone states and locations we would require to declare some permissions in manifest:

Next lets define our PhoneStateListenerActivity

In this class one can see that in the tManager.listen method a new object for CustomPhoneStateListener is defined and in the events parameter a bitwise-OR combination of Android PhoneStateListener LISTEN_ flags is passed. Now lets have a look at some of the main methods of CustomPhoneStateListener.

Here in the above class I extended the Android PhoneStateListener class and I am overriding some of its key methods. Through these overridden methods we can access the telephony states, cell locations, network types, incoming call number and much more network carrier related information. In this Android PhoneStateListener Example I’ll be explaining all key methods in short except two key callback methods as they have separate tutorials. First one onDataConnectionStateChanged and second one onSignalStrengthsChanged. To keep it simple and self explanatory I am just using logs to display the all the states. Now lets start with first overridden method:

Android PhoneStateListener onDataActivity

This method is called when some data activity is observed like upload or download or both. Whenever there is a change in network data you would receive a callback here.

Android PhoneStateListener onServiceStateChanged

This method is called when something changes in network operator service. It could be a change in service when phone goes from in service to out of service or to emergency only. In this callback method, along with states some other parameters are returned like operator name from getOperatorAlphaLong method. To detect if phone is in roaming getRoaming method can be used.

Android PhoneStateListener onCallStateChanged

Well this method is one of the most used method of this class as this method listens to the call state changes. Like when phone starts ringing, call is put on hold or a call is ended.

Android PhoneStateListener onCellLocationChanged

This method is called when your device switches operator cell location. This method can provide great amounts of information regarding the location of device as its object can be cast to GsmCellLocation or CdmaCellLocation. Once we have location area code through GsmCellLocation.getLac() method and cell id through GsmCellLocation.getCid() method we can query and get actual latitude and longitude of that location by any web-service freely available on internet. Also would like to mention here that in case of CDMA device this process is not required as CdmaCellLocation class contains the latitude and longitude parameters in itself.

Concluding this tutorial I would like to mention separate tutorials are available for more methods like onSignalStrengthsChanged and onDataConnectionStateChanged. Please share this tutorial if it helped you.

About Mohit Gupt

An android enthusiast, and an iPhone user with a keen interest in development of innovative applications.


Leave a comment

Your email address will not be published. Required fields are marked *

One thought on “Android PhoneStateListener Example