Android Tutorial

(avery) #1

By : Ketan Bhimani


396 

Requesting Call State

You can use the TelephonyManager object to retrieve the state of
the phone and some information about the phone service itself,
such as the phone number of the handset.

You can request an instance of TelephonyManager using the
getSystemService() method, like this:

TelephonyManager telManager = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);


With a valid TelephonyManager instance, an application can now
make several queries.One important method is getCallState().This
method can determine the voice call status of the handset. The
following block of code shows how to query for the call state and all
the possible return values:

int callStatus = telManager.getCallState();
String callState = null;
switch (callStatus) {
case TelephonyManager.CALL_STATE_IDLE:
callState = “Phone is idle.”;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
callState = “Phone is in use.”;
break;
case TelephonyManager.CALL_STATE_RINGING:
callState = “Phone is ringing!”;
break;
}
Log.i(“telephony”, callState);


The three call states can be simulated with the emulator through
the Dalvik Debug Monitor

Querying for the call state can be useful in certain circumstances.
However, listening for changes in the call state can enable an
application to react appropriately to something the user might be
Free download pdf