Using the Telephony API

Posted on Jan 13, 2010 (4 years ago). Seen 31,218 times. 5 comments. Permalink Feed
Member since Dec 23, 2009
Location: Oakland
Stream Posts: 0
Tagged as: Android Tutorial

The telephony API is used to among other things monitor phone information including the current states of the phone, connections, network etc.

In this example, we want to utilize the telephone manager part of the Application Framework and use phone listeners (PhoneStateListener) to retrieve various phone states.

This is a simple tutorial utilizing the telephony API and its associated listener which can be good before implementing other application functions related to a phone state like connecting the Call.

This is a small example that can be used expanded with the various methods available, please see http://developer.android.com/reference/android/telephony/package-summary.html


TelephonyDemo.java
Code:

package com.marakana;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.TextView;

public class TelephonyDemo extends Activity {
TextView textOut;
TelephonyManager telephonyManager;
PhoneStateListener listener;

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

// Get the UI
textOut = (TextView) findViewById(R.id.textOut);

// Get the telephony manager
telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

// Create a new PhoneStateListener
listener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
String stateString = "N/A";
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
stateString = "Idle";
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
stateString = "Off Hook";
break;
case TelephonyManager.CALL_STATE_RINGING:
stateString = "Ringing";
break;
}
textOut.append(String.format("\nonCallStateChanged: %s", stateString));
}
};

// Register the listener wit the telephony manager
telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
}


main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Telephony Demo"
android:textSize="22sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textOut"
android:text="Output"></TextView>
</LinearLayout>


Output

Source
http://thenewcircle.com/static/tutorials/TelephonyDemo.zip

Comments

Posted on May 13, 2010
Photo Joao Marco
None
Member since May 13, 2010
Hi Serete,

Nice post! Were you able to auto-accept incoming calls?
If so, could you post a sample code of it?
Thank you very much once again.
Posted on Oct 25, 2010
Photo Janardhanan S
Androidpeople
Member since Oct 25, 2010
Hi Serete,

Thanks for the post.

@ Joao

We can only get the call state.
We cant alter the call state.
Posted on Oct 25, 2010
Photo Marko Gargenta
@MarkoGargenta
Member since Jan 19, 2007
Location: San Francisco
You can - pick up the phone and make a call. Or have someone call you. That will change the phone state for sure :)
Posted on Feb 22, 2011
Photo Rafael Alpuche
Six Inc.
Member since Feb 22, 2011
Hi, thank you for the example.

I´m looking for some way to get the phone number you calling when TelephonyManager.CALL_STATE_OFFHOOK: occurs. There is a way to get it?

The incomingNumber just work when state change to TelephonyManager.CALL_STATE_RINGING, but what do i need to get the "outgoingNumber"?

Thanks again =)
Posted on Dec 14, 2011
Photo Sai Kumar
sai technologies
Member since Dec 10, 2011
how to use smsmanager to send data,and what are pending intents