Using PhoneStateListener to listen to state change in Android

Download source code of this article.

My previous article explains how to use the TelephonyManager. One of the important functionality of TelephonyManager is listening to different phone state events. The method listen(PhoneStateListener listener, int events) is used to add a phone state listener. The first parameter is the PhoneStateListener class and the second parameter is the int value contains various phone state to listen. We can listen for following events:

PhoneStateListener.LISTEN_SIGNAL_STRENGTH
PhoneStateListener.LISTEN_DATA_ACTIVITY
PhoneStateListener.LISTEN_CELL_LOCATION
PhoneStateListener.LISTEN_CALL_STATE
PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR
PhoneStateListener.LISTEN_DATA_CONNECTION_STATE
PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR
PhoneStateListener.LISTEN_SERVICE_STATE

The PhoneStateListener has following methods:

void onCallForwardingIndicatorChanged(boolean cfi)
void onCallStateChanged(int state, String incomingNumber)
void onCellLocationChanged(CellLocation location)
void onDataActivity(int direction)
void onDataConnectionStateChanged(int state)
void onDataConnectionStateChanged(int state, int networkType)
void onMessageWaitingIndicatorChanged(boolean mwi)
void onServiceStateChanged(ServiceState serviceState)
void onSignalStrengthChanged(int asu)
void onSignalStrengthsChanged(SignalStrength signalStrength)

The PhoneStateListener.LISTEN_SIGNAL_STRENGTH events invoke the onSignalStrengthChanged(int asu) method. This method requires the permission READ_PHONE_STATE. The method has one integer parameter which is the signal strength value. This value can be in the range 0-31. This function is deprecated in Android Version 2.0 and above. Above 2.0 we should use the onSignalStrengthsChanged(SignalStrength signalStrength) method. Only one parameter is there and it SignalStrenth class. The SignalStrength class has different following methods:

int describeContents()
boolean equals(Object o)
int getCdmaDbm()
int getCdmaEcio()
int getEvdoDbm()
int getEvdoEcio()
int getEvdoSnr()
int getGsmBitErrorRate()
int getGsmSignalStrength()

The function names describes its purpose.

The PhoneStateListener.LISTEN_DATA_CONNECTION_STATE event invokes the methods onDataConnectionStateChanged(int state) and onDataConnectionStateChanged(int state, int networkType). Both these are invoked. The first parameter is the connection state and can be one of the following values:

TelephonyManager.DATA_DISCONNECTED
TelephonyManager.DATA_CONNECTING
TelephonyManager.DATA_CONNECTED
TelephonyManager.DATA_SUSPENDED

The PhoneStateListener.LISTEN_DATA_ACTIVITY event invokes the method onDataActivity(int direction). This method requires the permission READ_PHONE_STATE. The direction parameter can be one of the following values:

TelephonyManager.DATA_ACTIVITY_IN
TelephonyManager.DATA_ACTIVITY_OUT
TelephonyManager.DATA_ACTIVITY_INOUT
TelephonyManager.DATA_ACTIVITY_NONE

The PhoneStateListener.LISTEN_CALL_STATE event invokes the method onCallStateChanged(int state, String incomingNumber). This method requires the permission READ_PHONE_STATE. The state parameter can be one of the following values:

TelephonyManager.CALL_STATE_IDLE
TelephonyManager.CALL_STATE_RINGING
TelephonyManager.CALL_STATE_OFFHOOK

The incomingNumber parameter contains the incoming number if the state is TelephonyManager.CALL_STATE_RINGING.

The PhoneStateListener.LISTEN_CELL_LOCATION event invokes the method onCellLocationChanged(CellLocation location) method. This method requires the permission ACCESS_COARSE_LOCATION. This will be an instance of GsmCellLocation class. GsmCellLocation class is explained in my previous article.

The PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR event invokes the method onCallForwardingIndicatorChanged(boolean cfi) method. This method requires the permission READ_PHONE_STATE. The boolean parameter cgi indicates whether the current call is forwarding or not.

The PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR event invokes the method onMessageWaitingIndicatorChanged(boolean mwi). This method requires the permission READ_PHONE_STATE. The boolean parameter mwi indicates whether an incoming message is waiting for user attention or not.

The PhoneStateListener.LISTEN_SERVICE_STATE event invokes the method onServiceStateChanged(ServiceState serviceState). The parameter is an instance of ServiceState class. The ServiceState class has methods to retrieve operator name in short alphanumeric format, operator name in short long format, operator id, etc…

To un-register a listener we use the method listen with event parameter set to LISTEN_NONE.

The sample provided is same for this and the previous article. Hope this article helps to to get started with PhoneStateListener.

2 thoughts on “Using PhoneStateListener to listen to state change in Android

  1. Pingback: Android Helpful links | Farhan's Blogs & Portal

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>