FileDocCategorySizeDatePackage
PhoneStateListener.javaAPI DocAndroid 5.1 API21128Thu Mar 12 22:22:42 GMT 2015android.telephony

PhoneStateListener

public class PhoneStateListener extends Object
A listener class for monitoring changes in specific telephony states on the device, including service state, signal strength, message waiting indicator (voicemail), and others.

Override the methods for the state that you wish to receive updates for, and pass your PhoneStateListener object, along with bitwise-or of the LISTEN_ flags to {@link TelephonyManager#listen TelephonyManager.listen()}.

Note that access to some telephony information is permission-protected. Your application won't receive updates for protected information unless it has the appropriate permissions declared in its manifest file. Where permissions apply, they are noted in the appropriate LISTEN_ flags.

Fields Summary
private static final String
LOG_TAG
private static final boolean
DBG
public static final int
LISTEN_NONE
Stop listening for updates.
public static final int
LISTEN_SERVICE_STATE
Listen for changes to the network service state (cellular).
public static final int
LISTEN_SIGNAL_STRENGTH
Listen for changes to the network signal strength (cellular). {@more} Requires Permission: {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}

public static final int
LISTEN_MESSAGE_WAITING_INDICATOR
Listen for changes to the message-waiting indicator. {@more} Requires Permission: {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}

Example: The status bar uses this to determine when to display the voicemail icon.

public static final int
LISTEN_CALL_FORWARDING_INDICATOR
Listen for changes to the call-forwarding indicator. {@more} Requires Permission: {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
public static final int
LISTEN_CELL_LOCATION
Listen for changes to the device's cell location. Note that this will result in frequent callbacks to the listener. {@more} Requires Permission: {@link android.Manifest.permission#ACCESS_COARSE_LOCATION ACCESS_COARSE_LOCATION}

If you need regular location updates but want more control over the update interval or location precision, you can set up a listener through the {@link android.location.LocationManager location manager} instead.

public static final int
LISTEN_CALL_STATE
Listen for changes to the device call state. {@more} Requires Permission: {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
public static final int
LISTEN_DATA_CONNECTION_STATE
Listen for changes to the data connection state (cellular).
public static final int
LISTEN_DATA_ACTIVITY
Listen for changes to the direction of data traffic on the data connection (cellular). {@more} Requires Permission: {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE} Example: The status bar uses this to display the appropriate data-traffic icon.
public static final int
LISTEN_SIGNAL_STRENGTHS
Listen for changes to the network signal strengths (cellular).

Example: The status bar uses this to control the signal-strength icon.

public static final int
LISTEN_OTASP_CHANGED
Listen for changes to OTASP mode.
public static final int
LISTEN_CELL_INFO
Listen for changes to observed cell info.
public static final int
LISTEN_PRECISE_CALL_STATE
Listen for precise changes and fails to the device calls (cellular). {@more} Requires Permission: {@link android.Manifest.permission#READ_PRECISE_PHONE_STATE READ_PRECISE_PHONE_STATE}
public static final int
LISTEN_PRECISE_DATA_CONNECTION_STATE
Listen for precise changes and fails on the data connection (cellular). {@more} Requires Permission: {@link android.Manifest.permission#READ_PRECISE_PHONE_STATE READ_PRECISE_PHONE_STATE}
public static final int
LISTEN_DATA_CONNECTION_REAL_TIME_INFO
Listen for real time info for all data connections (cellular)). {@more} Requires Permission: {@link android.Manifest.permission#READ_PRECISE_PHONE_STATE READ_PRECISE_PHONE_STATE}
public static final int
LISTEN_VOLTE_STATE
Listen for changes to LTE network state
public static final int
LISTEN_OEM_HOOK_RAW_EVENT
Listen for OEM hook raw event
protected int
mSubId
private final android.os.Handler
mHandler
com.android.internal.telephony.IPhoneStateListener
callback
The callback methods need to be called on the handler thread where this object was created. If the binder did that for us it'd be nice.
Constructors Summary
public PhoneStateListener()
Create a PhoneStateListener for the Phone with the default subscription. This class requires Looper.myLooper() not return null. To supply your own non-null looper use PhoneStateListener(Looper looper) below.


                                    
      
        this(SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, Looper.myLooper());
    
public PhoneStateListener(android.os.Looper looper)
Create a PhoneStateListener for the Phone with the default subscription using a particular non-null Looper.

hide

        this(SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, looper);
    
public PhoneStateListener(int subId)
Create a PhoneStateListener for the Phone using the specified subscription. This class requires Looper.myLooper() not return null. To supply your own non-null Looper use PhoneStateListener(int subId, Looper looper) below.

hide

        this(subId, Looper.myLooper());
    
public PhoneStateListener(int subId, android.os.Looper looper)
Create a PhoneStateListener for the Phone using the specified subscription and non-null Looper.

hide

        if (DBG) log("ctor: subId=" + subId + " looper=" + looper);
        mSubId = subId;
        mHandler = new Handler(looper) {
            public void handleMessage(Message msg) {
                if (DBG) {
                    log("mSubId=" + mSubId + " what=0x" + Integer.toHexString(msg.what)
                            + " msg=" + msg);
                }
                switch (msg.what) {
                    case LISTEN_SERVICE_STATE:
                        PhoneStateListener.this.onServiceStateChanged((ServiceState)msg.obj);
                        break;
                    case LISTEN_SIGNAL_STRENGTH:
                        PhoneStateListener.this.onSignalStrengthChanged(msg.arg1);
                        break;
                    case LISTEN_MESSAGE_WAITING_INDICATOR:
                        PhoneStateListener.this.onMessageWaitingIndicatorChanged(msg.arg1 != 0);
                        break;
                    case LISTEN_CALL_FORWARDING_INDICATOR:
                        PhoneStateListener.this.onCallForwardingIndicatorChanged(msg.arg1 != 0);
                        break;
                    case LISTEN_CELL_LOCATION:
                        PhoneStateListener.this.onCellLocationChanged((CellLocation)msg.obj);
                        break;
                    case LISTEN_CALL_STATE:
                        PhoneStateListener.this.onCallStateChanged(msg.arg1, (String)msg.obj);
                        break;
                    case LISTEN_DATA_CONNECTION_STATE:
                        PhoneStateListener.this.onDataConnectionStateChanged(msg.arg1, msg.arg2);
                        PhoneStateListener.this.onDataConnectionStateChanged(msg.arg1);
                        break;
                    case LISTEN_DATA_ACTIVITY:
                        PhoneStateListener.this.onDataActivity(msg.arg1);
                        break;
                    case LISTEN_SIGNAL_STRENGTHS:
                        PhoneStateListener.this.onSignalStrengthsChanged((SignalStrength)msg.obj);
                        break;
                    case LISTEN_OTASP_CHANGED:
                        PhoneStateListener.this.onOtaspChanged(msg.arg1);
                        break;
                    case LISTEN_CELL_INFO:
                        PhoneStateListener.this.onCellInfoChanged((List<CellInfo>)msg.obj);
                        break;
                    case LISTEN_PRECISE_CALL_STATE:
                        PhoneStateListener.this.onPreciseCallStateChanged((PreciseCallState)msg.obj);
                        break;
                    case LISTEN_PRECISE_DATA_CONNECTION_STATE:
                        PhoneStateListener.this.onPreciseDataConnectionStateChanged(
                                (PreciseDataConnectionState)msg.obj);
                        break;
                    case LISTEN_DATA_CONNECTION_REAL_TIME_INFO:
                        PhoneStateListener.this.onDataConnectionRealTimeInfoChanged(
                                (DataConnectionRealTimeInfo)msg.obj);
                        break;
                    case LISTEN_VOLTE_STATE:
                        PhoneStateListener.this.onVoLteServiceStateChanged((VoLteServiceState)msg.obj);
                        break;
                    case LISTEN_OEM_HOOK_RAW_EVENT:
                        PhoneStateListener.this.onOemHookRawEvent((byte[])msg.obj);
                        break;

                }
            }
        };
    
Methods Summary
private voidlog(java.lang.String s)


        
        Rlog.d(LOG_TAG, s);
    
public voidonCallForwardingIndicatorChanged(boolean cfi)
Callback invoked when the call-forwarding indicator changes.

        // default implementation empty
    
public voidonCallStateChanged(int state, java.lang.String incomingNumber)
Callback invoked when device call state changes.

see
TelephonyManager#CALL_STATE_IDLE
see
TelephonyManager#CALL_STATE_RINGING
see
TelephonyManager#CALL_STATE_OFFHOOK

        // default implementation empty
    
public voidonCellInfoChanged(java.util.List cellInfo)
Callback invoked when a observed cell info has changed, or new cells have been added or removed.

param
cellInfo is the list of currently visible cells.

    
public voidonCellLocationChanged(android.telephony.CellLocation location)
Callback invoked when device cell location changes.

        // default implementation empty
    
public voidonDataActivity(int direction)
Callback invoked when data activity state changes.

see
TelephonyManager#DATA_ACTIVITY_NONE
see
TelephonyManager#DATA_ACTIVITY_IN
see
TelephonyManager#DATA_ACTIVITY_OUT
see
TelephonyManager#DATA_ACTIVITY_INOUT
see
TelephonyManager#DATA_ACTIVITY_DORMANT

        // default implementation empty
    
public voidonDataConnectionRealTimeInfoChanged(DataConnectionRealTimeInfo dcRtInfo)
Callback invoked when data connection state changes with precise information.

hide

        // default implementation empty
    
public voidonDataConnectionStateChanged(int state)
Callback invoked when connection state changes.

see
TelephonyManager#DATA_DISCONNECTED
see
TelephonyManager#DATA_CONNECTING
see
TelephonyManager#DATA_CONNECTED
see
TelephonyManager#DATA_SUSPENDED

        // default implementation empty
    
public voidonDataConnectionStateChanged(int state, int networkType)
same as above, but with the network type. Both called.

    
public voidonMessageWaitingIndicatorChanged(boolean mwi)
Callback invoked when the message-waiting indicator changes.

        // default implementation empty
    
public voidonOemHookRawEvent(byte[] rawData)
Callback invoked when OEM hook raw event is received. Requires the READ_PRIVILEGED_PHONE_STATE permission.

param
rawData is the byte array of the OEM hook raw data.
hide

        // default implementation empty
    
public voidonOtaspChanged(int otaspMode)
The Over The Air Service Provisioning (OTASP) has changed. Requires the READ_PHONE_STATE permission.

param
otaspMode is integer OTASP_UNKNOWN=1 means the value is currently unknown and the system should wait until OTASP_NEEDED=2 or OTASP_NOT_NEEDED=3 is received before making the decision to perform OTASP or not.
hide

        // default implementation empty
    
public voidonPreciseCallStateChanged(android.telephony.PreciseCallState callState)
Callback invoked when precise device call state changes.

hide

        // default implementation empty
    
public voidonPreciseDataConnectionStateChanged(android.telephony.PreciseDataConnectionState dataConnectionState)
Callback invoked when data connection state changes with precise information.

hide

        // default implementation empty
    
public voidonServiceStateChanged(android.telephony.ServiceState serviceState)
Callback invoked when device service state changes.

see
ServiceState#STATE_EMERGENCY_ONLY
see
ServiceState#STATE_IN_SERVICE
see
ServiceState#STATE_OUT_OF_SERVICE
see
ServiceState#STATE_POWER_OFF

        // default implementation empty
    
public voidonSignalStrengthChanged(int asu)
Callback invoked when network signal strength changes.

see
ServiceState#STATE_EMERGENCY_ONLY
see
ServiceState#STATE_IN_SERVICE
see
ServiceState#STATE_OUT_OF_SERVICE
see
ServiceState#STATE_POWER_OFF
deprecated
Use {@link #onSignalStrengthsChanged(SignalStrength)}

        // default implementation empty
    
public voidonSignalStrengthsChanged(android.telephony.SignalStrength signalStrength)
Callback invoked when network signal strengths changes.

see
ServiceState#STATE_EMERGENCY_ONLY
see
ServiceState#STATE_IN_SERVICE
see
ServiceState#STATE_OUT_OF_SERVICE
see
ServiceState#STATE_POWER_OFF

        // default implementation empty
    
public voidonVoLteServiceStateChanged(android.telephony.VoLteServiceState stateInfo)
Callback invoked when the service state of LTE network related to the VoLTE service has changed.

param
stateInfo is the current LTE network information
hide