Example Determining the State of a Call

This section shows how to track the state of a phone call. It adds some of the classes described in the previous section to the application shown in "Creating an Example Application to Run the call Method" on page 278, which finds and uses PhoneApp to start and control a phone call. Here, in addition to starting a phone call and letting PhoneApp control it, the application gets some information about the call as it is happening.

In order to get this information, we need to extend the PhoneStateListener class, make an instance of our subclass, and pass that instance to the TelephonyManager.listen method. Example 15-1 shows the code for our subclass of the PhoneStateListener class.

Example 15-1. Defining a Listener for telephone call state private class ListenToPhoneState extends PhoneStateListener {

public void onCallStateChanged(int state, String incomingNumber) { Log.i("telephony-example", "State changed: " + stateName(state));

String stateName(int state) { switch (state) {

case TelephonyManager.CALL_STATE_IDLE: return "Idle"; case TelephonyManager.CALL_STATE_OFFHOOK: return "Off hook"; case TelephonyManager.CALL_STATE_RINGING: return "Ringing";

return Integer.toString(state);

The lines we've highlighted are:

O Overrides the onCallStateChanged method of the PhoneStateListener class. © Adds a message to Android's log whenever the state changes. © Chooses meaningful strings to represent call states.

In this subclass of the PhoneStateListener class, we override the onCallStateChanged method, which Android calls when a call's state changes. We use the Log class, a static

Operations Offered by the android.telephony Package | 289

class with utility methods for logging information, to log the changes as Android passes them to us.

Finally, our stateName method decodes the states that correspond to the constants defined in the TelephonyManager class to make the log more readable.

Returning to our main application from "Creating an Example Application to Run the call Method" on page 278, we have to change it by creating a Listener and assigning our subclass of PhoneStateListener to it. Here is the code for the entire modified example application, which now tracks and logs state transitions: package example.telephony;

import android.app.Activity;

import android.content.ActivityNotFoundException; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.util.Log;

public class telephonyExplorer extends Activity { ListenToPhoneState listener;©

/** Called when the activity is first created. */ ^Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); call();

Intent callIntent = new Intent(Intent.ACTION_CALL);

callIntent.setData(Uri.parse("tel:978555l212"));

startActivity(callIntent);

TelephonyManager tManager = (TelephonyManager)

getSystemService(Context.TELEPHONY_SERVICE);© listener = new ListenToPhoneState();0

tManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE); } catch (ActivityNotFoundException activityException) {

Log.e("telephony-example", "Call failed", activityException);

private class ListenToPhoneState extends PhoneStateListener {

public void onCallStateChanged(int state, String incomingNumber) { Log.i("telephony-example", "State changed: " + stateName(state));

String stateName(int state) { switch (state) {

case TelephonyManager.CALL_STATE_IDLE: return "Idle"; case TelephonyManager.CALL_STATE_OFFHOOK: return "Off hook"; case TelephonyManager.CALL_STATE_RINGING: return "Ringing";

return Integer.toString(state);

The lines we've highlighted are:

O New classes that must be imported to add a Listener for the telephone call state. © Adds a definition for the Listener. © Connects to Android's telephone call manager.

O Assigns our extended Listener class (defined at item 5) to the variable defined in item 2.

© Code from Example 15-1, defining our Listener.

Running this application results in output to the log window in Eclipse that should look something like this:

11-19 01:47:03.704: INFO/telephony-example(159): State changed: Idle 11-19 01:47:04.774: INFO/telephony-example(159): State changed: Off hook

0 0

Responses

  • retu
    How to manual call onCallStateChanged event on android Phone?
    7 years ago

Post a comment