Android Telephony

 

 In this blog post, we are going to take a look at Android's Telephony. To start off basic, lets talk about creating a call. One way to start a call in Android using Android's Telephony is to basically send a message to the Phone Application telling it to call a number.


1Intent callIntent = 
2    new Intent(Intent.ACTION_CALL, Uri.parse("tel:5555")); 
3startActivity(callIntent);

This is basically the extent at which you can actually control phone calls using Android's Telephony. There is not a public facing API in order to end a call or use any other features that are provided. In the next blog post, I will speak about Cisco's Call Control APIs that will let you perform more actions on a phone call.



In addition to being able to make a phone call, we can listen to the different state events from the phone. Android's Telephony provides three states which are Ringing, Offhook, and Idle. The Ringing state is anytime when the phone is either currently ringing or trying to call another person. The Offhook state is when a person answers the phone. The Idle state is when a person hangs up the call. As you can see from these states it is hard to tell which direction the phone call is coming, with the new Cisco Call Control APIs this has been improved upon. 

;

Now, let's take a look at what some of the actual code would look like. First, we need to get their Telephony Manager which is as follows:



1TelephonyManager telephonyManager = 
2    (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

We then need to create a PhoneStateListener that will perform actions when a state is changed.

 1PhoneStateListener listener = new PhoneStateListener() {
 2    @Override
 3    public void onCallStateChanged(int state, String incomingNumber) {
 4        switch (state) {
 5        case TelephonyManager.CALL_STATE_IDLE:
 6            Log.d("PhoneState", "Idle");
 7            break;
 8        case TelephonyManager.CALL_STATE_OFFHOOK:
 9            Log.d("PhoneState", "Offhook");    
10            break;
11        case TelephonyManager.CALL_STATE_RINGING:
12            Log.d("PhoneState", "Ringing");
13            break;
14        }
15    }
16};

In the above code snippet, we can see that we created a Phone State Listener and overrode the onCallStateChanged method. Within that method, we checked to see what state the phone changed to and then logged that state in this basic sample of what a Phone State Listener looks like. Then we need to make our Telephony Manager register to this listener by the following:



1telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);

With this we are basically done. We created a Telephony Manager and a Phone State Listener. Then proceeded to register the listener with that Telephony Manager. All that is left is to make sure to include in your manifest the permission to read the phone state.



This was a basic example of what can be achieved with Android's Telephony. For a slightly more in depth example, you can take a look at the source code. This source code is an example that pops up a dialog every time a call is finished in order to record some details about that phone call. In our next How-To, we will take a look at the Cisco Call Control APIs that were recently released with the Cisco Cius Android Add-on. 



By MatthewT Williams

 

Subscribe to our Cius Developer Blog RSS feed to get the latest info and sample code.