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>

Wednesday, August 8, 2012

Thread in android | Thread demo in android | How to create a Thread in Android | Handler Class In Android


Hello Dear Friends,
    Some to we got when we create a Thread in android because activity class is also a thread class for facing this problem we should use Handler class. A little example of handler class are given below-

// create thread
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
// call method
add();
}
//sleep thread for 15 mili second
}, 1500);