Using Telephony API

Forums » Android - Examples > Using Telephony API
January 13, 2010 1:51:25 PM PST (3 years ago). Seen 28,838 times. 5 replies.
Photo Serete Itebete
Member since Dec 23, 2009
Location: Oakland
Forum Posts: 17
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://marakana.com/static/tutorials/TelephonyDemo.zip
Edited 4 times. Last edit by Rafael Alpuche on Feb 22, 2011 at 8:36:53 AM (about one year ago).
May 13, 2010 1:50:28 AM PDT (3 years ago)
Photo Joao Marco
None
Member since May 13, 2010
Forum Posts: 1
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.
October 25, 2010 3:59:42 AM PDT (2 years ago)
Photo Janardhanan S
Androidpeople
Member since Oct 25, 2010
Forum Posts: 1
Hi Serete,

Thanks for the post.

@ Joao

We can only get the call state.
We cant alter the call state.
October 25, 2010 1:12:09 PM PDT (2 years ago)
Photo Marko Gargenta
@MarkoGargenta
Marakana, Inc.
Member since Jan 19, 2007
Location: San Francisco
Forum Posts: 227
You can - pick up the phone and make a call. Or have someone call you. That will change the phone state for sure :)
February 22, 2011 8:36:53 AM PST (2 years ago)
Photo Rafael Alpuche
Six Inc.
Member since Feb 22, 2011
Forum Posts: 1
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 =)

December 14, 2011 1:07:27 AM PST (one year ago)
Photo Sai Kumar
sai technologies
Member since Dec 10, 2011
Forum Posts: 1
how to use smsmanager to send data,and what are pending intents
Edited one time. Last edit by Sai Kumar on Dec 14, 2011 at 1:09:54 AM (about one year ago).