Android Demo: Telephony Manager

Source: http://code-lab.cmalhotra.com/android-demo-telephony-manager/

Hello Everyone,

This post shows a very simplified code to be used to fetch Call State using Telephony manager:

Manifest.xml

<?xml version=“1.0″ encoding=“utf-8″?>
<manifest xmlns:android=“http://schemas.android.com/apk/res/android”
    package=“com.codelab.telephony”
    android:versionCode=“1″
    android:versionName=“1.0″ >
    <uses-sdk android:minSdkVersion=“15″ />
    <uses-permission android:name=“android.permission.READ_PHONE_STATE” />
    <application
        android:icon=“@drawable/ic_launcher”
        android:label=“@string/app_name” >
        <activity
            android:label=“@string/app_name”
            android:name=“.TelephonyManagerDemoActivity” >
            <intent-filter >
                <action android:name=“android.intent.action.MAIN” />
                <category android:name=“android.intent.category.LAUNCHER” />
            </intent-filter>
        </activity>
    </application>
</manifest>

TelephonyManagerDemoActivity.java

package com.codelab.telephony;

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 TelephonyManagerDemoActivity extends Activity {
        TextView textView;
         TelephonyManager telephonyManager;
         PhoneStateListener listener;
         /** Called when the activity is first created. */
         @Override
         public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.main);
           textView = (TextView) findViewById(R.id.text1);
           // 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;
               }
               textView.append(String.format(“\nCallState: %s”, stateString));
             }
           };
           // Register the listener wit the telephony manager
           telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
         }
       }

Main.xml

<?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 Manager Demo”
              android:textSize=“22sp” />
       <TextView
              android:layout_width=“wrap_content”
              android:layout_height=“wrap_content”
              android:id=“@+id/text1″
              android:text=“Result”></TextView>
</LinearLayout>
Advertisements

Published by

rockaclimba

Trekking, Climbing, Camping, Reading, Music,

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s