Wednesday, August 22, 2012

Telephony Manager Demo in Android


Hello Friends!
                Today I am going to share very simple code for getting call state in Android using Telephony Manager.

1-PrintScreen:




2-manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.manish.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>

3-TelephonyManagerDemoActivity.java

package com.manish.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);
         }
       }

4-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>

No comments :

Post a Comment

Note: Only a member of this blog may post a comment.