Recipe Listening for Phone States
The PhoneStateListener class provides information about the different telephony states on the device, including network service state, signal strength, and message waiting indicator (voicemail). Some require an explicit permission as shown in Table 7.2.
Table 7.2 The Possible Phone State Listener Events and Required Permissions
Phone State Listener
LISTEN_CALL_FORWARDING_ INDICATOR
LISTEN CALL STATE
LISTEN_CELL_LOCATION LISTEN_DATA_ACTIVITY
LISTEN_DATA_CONNECTION_ STATE
LISTEN_MESSAGE_WAITING_ INDICATOR
LISTEN_NONE LISTEN_SERVICE_STATE
LISTEN SIGNAL STRENGTHS
Description
Listen for call forward indicator changes
Listen for call state changes
Listen for cell location changes
Listen for direction of data traffic on cellular changes Listen for data connection state changes
Listen for message waiting indicator changes
Remove listeners
Listen for network service state changes Listen for network signal strength changes
Permission
READ_PHONE_STATE READ_PHONE_STATE
ACCESS_COARSE_ LOCATION
READ_PHONE_STATE
None
READ_PHONE_STATE
None None
None
For example, to listen for an incoming call, the TelephonyManager needs to register a listener for the PhoneStateListener.LISTEN_CALL_STATE event.The three possible call states are
■ CALL_STATE_IDLE—Device not being used for a phone call
■ CALL_STATE_ringing—Device receiving a call
■ CALL_STATE_OFFHOOK—Call in progress
This recipe lists the phone call state changes as they occur. By using the Logcat tool (discussed in Chapter 12,"Debugging"), these different states can be seen when an incoming call or outgoing call occurs.
The main activity is shown in Listing 7.9. It creates a new inner class extending the PhoneStateListener, which overrides the onCallStateChanged method to catch the phone call state changes. Other methods that can be overridden are onCallForwardingIndicator(), onCellLocationChanged(), and onDataActivity().
Listing 7.9 src/com/cookbook/hardware.telephony/HardwareTelephony.java package com.cookbook.hardware.telephony;
import android.app.Activity; import android.os.Bundle;
import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager;
import android.util.Log; import android.widget.TextView;
public class HardwareTelephony extends Activity { TextView tvl;
TelephonyManager telManager;
^Override public void onCreate(Bundle savedlnstanceState) { super.onCreate(savedlnstanceState); setContentView(R.layout.main); tvl =(TextView) findViewByld(R.id.tvl); telManager = (TelephonyManager)
getSystemService(TELEPHONY_SERVICE);
telManager.listen(new TelListener(),
PhoneStateListener.LISTEN_CALL_STATE);
private class TelListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) { super.onCallStateChanged(state, incomingNumber);
Log.v("Phone State", "state:"+state); switch (state) {
case TelephonyManager.CALL_STATE_IDLE: Log.v("Phone State",
"incomingNumber:"+incomingNumber+"
break;
case TelephonyManager.CALL_STATE_OFFHOOK: Log.v("Phone State",
"incomingNumber:"+incomingNumber+"
break;
case TelephonyManager.CALL_STATE_RINGING: Log.v("Phone State",
"incomingNumber:"+incomingNumber+"
break; default: break;
Post a comment