Showing posts with label Incoming Calls. Show all posts
Showing posts with label Incoming Calls. Show all posts

Thursday, May 9, 2013

Incoming Call Broadcast Reciever


Learn Android Development


In previous I discussed about how to handle incoming call or how to listen for Incoming call using Activity, In this post I will discuss the same using Broadcast Receiver.

Broadcast Receiver is  better way than Activity to listen for Incoming Calls.

So declare the Permission and Receiver in Manifest

Permission Required:

 <uses-permission android:name="android.permission.READ_PHONE_STATE" />

Declare the receiver and register it to listen "android.intent.action.PHONE_STATE"  action

android.manifest 


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.incomingcallreciever"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />
   
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
       
       
        <receiver android:name=".IncommingCallReceiver" android:enabled="true">
            <intent-filter>
                                     <action android:name="android.intent.action.PHONE_STATE" />
                </intent-filter>
           </receiver>

    </application>

</manifest>







Incoming Call Receiver




import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.widget.Toast;


 public class IncommingCallReceiver extends BroadcastReceiver
 {
   
      Context mContext;
     
     
      @Override
      public void onReceive(Context mContext, Intent intent)
      {
          try
          {
          
              String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

          
           
              if(state.equals(TelephonyManager.EXTRA_STATE_RINGING))
              {
                   Toast.makeText(mContext, "Phone Is Ringing", Toast.LENGTH_LONG).show();
                   // Your Code
              }
             
              if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
              {
                   Toast.makeText(mContext, "Call Recieved", Toast.LENGTH_LONG).show();
                       // Your Code
              }
             
              if (state.equals(TelephonyManager.EXTRA_STATE_IDLE))
              {
               
                  Toast.makeText(mContext, "Phone Is Idle", Toast.LENGTH_LONG).show();
                      // Your Code
               
              }
          }
          catch(Exception e)
          {
              //your custom message
          }
      
     }
     
}






     
     
   

 

 

 

Advance Android Topics


                   Customizing Toast In Android 
                   Showing Toast for Longer Time
                   Customizing the Display Time of Toast
                   Using TimePickerDialog and DatePickerDialog In android
                   Animating A Button In Android
                    Populating ListView With DataBase

                    Customizing Checkboxes In Android 
                    Increasin Size of Checkboxes
                    Android ProgressBar
                    Designing For Different Screen Sizes
                    Handling Keyboard Events 



More Android Topics:



Android : Introduction


       Eclipse Setup for Android Development

                     Configuring Eclipse for Android Development

          Begging With Android

                     Creating Your First Android Project
                     Understanding Android Manifest File of your android app


         Working With Layouts

                      Understanding Layouts in Android
                          Working with Linear Layout (With Example)
                                Nested Linear Layout (With Example)
                          Table Layout
                          Frame Layout(With Example)
                         Absolute Layout
                         Grid Layout


       Activity

                     Activity In Android
                     Activity Life Cycle
                     Starting Activity For Result
                     Sending Data from One Activity to Other in Android
                     Returning Result from Activity

     Working With Views

                     Using Buttons and EditText in Android 
                     Using CheckBoxes in Android 
                     Using AutoCompleteTextView in Android
                     Grid View

       Toast

                     Customizing Toast In Android
                     Customizing the Display Time of Toast
                     Customizing Toast At Runtime
                     Adding Image in Toast
                     Showing Toast for Longer Time

     Dialogs In Android

                     Working With Alert Dialog
                     Adding Radio Buttons In Dialog
                     Adding Check Boxes In Dialog
                     Creating Customized Dialogs in Android
                    Adding EditText in Dialog

                   Creating Dialog To Collect User Input

                 DatePicker and TimePickerDialog

                              Using TimePickerDialog and DatePickerDialog In android

    Working With SMS

                  How to Send SMS in Android
                  How To Receive SMS
                  Accessing Inbox In Android

    ListView:

               Populating ListView With DataBase

      Menus In Android

                    Creating Option Menu
                    Creating Context Menu In Android

      TelephonyManager

                    Using Telephony Manager In Android

     Working With Incoming Calls

                    How To Handle Incoming Calls in Android
                    How to Forward an Incoming Call In Android
                   CALL States In Android
 

    Miscellaneous

                   Notifications In Android
                   How To Vibrate The Android Phone
                   Sending Email In Android
                  Opening a webpage In Browser
                   How to Access PhoneBook In Android
                   Prompt User Input with an AlertDialog

   Storage:  Storing Data In Android


               Shared Prefferences  In Android

                             SharedPreferences In Android

               Files: File Handling In Android

                              Reading and Writing files to Internal Stoarage
                              Reading and Writing files to SD Card 
                           

                DataBase : Working With Database

                             Working With Database in Android
                             Creating Table In Android
                             Inserting, Deleting and Updating Records In Table in Android
                             How to Create DataBase in Android
                             Accessing Inbox In Android

     Animation In Android:

                  Animating A Button In Android



Wednesday, March 20, 2013

CALL States In Android

In this Post I will describe how to Handle incoming Calls on Android and Reacting to them i.e. receiving, rejecting etc.

We  can do this by Activity and by Broadcast Receiver, here we will do using Activity.

Permissions: 
As I mentioned in my earlier posts we require to declare permission in manifest file.
For listening and reacting to Incoming calls we need following permissions

android.permission.READ_PHONE_STATE   to listen to InComing Calls
"android.permission.MODIFY_PHONE_STATE"   to receive Incoming Calls

You can learn more about permission  Here.

Add these permissions in your manifest file.
Your Manifest file should look like

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.smartdialer"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8"
              android:targetSdkVersion="15" />
   
    <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
       <uses-permission android:name="android.permission.READ_PHONE_STATE" />


 <application
        android:icon="@drawable/icon"
        android:label="@string/app_name" >
        <activity
            android:name=".HomeActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
       
       
           </application>

</manifest
>


Activity:

We need to use TelephonyManager class and  TELEPHONY_SERVICE.

The onCallStateChanged handler receives the phone number associated with incoming calls, and the
state parameter represents the current call state as one of the following three values:

➤ TelephonyManager.CALL_STATE_IDLE When the phone is neither ringing nor in a call

➤ TelephonyManager.CALL_STATE_RINGING When the phone is ringing

➤ TelephonyManager.CALL_STATE_OFFHOOK When the phone is currently in a call


How to Monitor Incoming Calls:


We need to have a listener to listen the InComing Calls

PhoneStateListener callStateListener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber) {
// TODO React to incoming call.
}
};
telephonyManager.listen(callStateListener,
PhoneStateListener.LISTEN_CALL_STATE);


Code:


public class GetCallerInfoActivity extends Activity
{


  @Override
            public void onCreate(Bundle savedInstanceState)
            {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.main);
                   
                                       TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
                    PhoneStateListener callStateListener = new PhoneStateListener() {
                    public void onCallStateChanged(int state, String incomingNumber)
                    {
                            // TODO React to incoming call.
                            number=incomingNumber;
                            if(state==TelephonyManager.CALL_STATE_RINGING)
                            {
                                    Toast.makeText(getApplicationContext(),"Phone Is Riging", Toast.LENGTH_LONG).show();
                                   
                            }
                            if(state==TelephonyManager.CALL_STATE_OFFHOOK)
                            {
                                Toast.makeText(getApplicationContext(),"Phone is Currently in A call", Toast.LENGTH_LONG).show();
                            }
                           
                           
                            if(state==TelephonyManager.CALL_STATE_IDLE)
                            {
                                Toast.makeText(getApplicationContext(),"phone is neither ringing nor in a call", Toast.LENGTH_LONG).show();
                            }
                    }
                    };
                    telephonyManager.listen(callStateListener,PhoneStateListener.LISTEN_CALL_STATE);
                   
                   
                   
            }
                   
                   
            }



How To Launch the Dialer to Initiate Phone Calls:


Use an Intent action to start a dialer activity; you should specify the number to dial using the tel: schema as the data component of the Intent.

Use the Intent.ACTION_DIAL Activity action to launch a dialer rather than dial the number immediately.
This action starts a dialer Activity, passing in the specified number but allowing the dialer application
to manage the call initialization (the default dialer asks the user to explicitly initiate the call). This
action doesn’t require any permissions and is the standard way applications should initiate calls.

Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:1234567"));
startActivity(intent);




Monday, November 26, 2012

How To Handle Incoming Calls in Android

The Android Development Tutorials blog contains Basic as well as Advanced android tutorials.Go to Android Development Tutorials to get list of all Android Tutorials.

Handle Incoming Calls in Android

In this Post I will describe how to Handle incoming Calls on Android and Reacting to them i.e. receiving, rejecting etc.

We  can do this by Activity and by Broadcast Receiver, here we will do using Activity.

Permissions: 
As I mentioned in my earlier posts we require to declare permission in manifest file.
For listening and reacting to Incoming calls we need following permissions

android.permission.READ_PHONE_STATE   to listen to InComing Calls
"android.permission.MODIFY_PHONE_STATE"   to receive Incoming Calls

You can learn more about permission  Here.

Add these permissions in your manifest file.
Your Manifest file should look like

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.smartdialer"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8"
              android:targetSdkVersion="15" />
   
    <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
       <uses-permission android:name="android.permission.READ_PHONE_STATE" />


 <application
        android:icon="@drawable/icon"
        android:label="@string/app_name" >
        <activity
            android:name=".HomeActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
       
       
           </application>

</manifest
>




How to Get IMEI number of the Phone



Some More Good  Android Topics
Customizing Toast In Android 
 Showing Toast for Longer Time
Customizing Checkboxes In Android 
Customizing Progress Bar
To learn Basic of Android Animation  go to  Android Animation Basics






Activity:

We need to use TelephonyManager class and  TELEPHONY_SERVICE.

The onCallStateChanged handler receives the phone number associated with incoming calls, and the
state parameter represents the current call state as one of the following three values:



➤ TelephonyManager.CALL_STATE_RINGING When the phone is ringing

 

 

 

 

 

 

 

➤ TelephonyManager.CALL_STATE_OFFHOOK When the phone is currently in a call means call is recieved









 

 

 

 

 

➤ TelephonyManager.CALL_STATE_IDLE When the phone is neither ringing nor in a call

 

 

 

 

 

 

 

 

 

 

 

 

How to Monitor Incoming Calls:


We need to have a listener to listen the InComing Calls

PhoneStateListener callStateListener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber) {
// TODO React to incoming call.
}
};
telephonyManager.listen(callStateListener,
PhoneStateListener.LISTEN_CALL_STATE);


Code:


public class GetCallerInfoActivity extends Activity
{


  @Override
            public void onCreate(Bundle savedInstanceState)
            {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.main);
                   
                    TelephonyManager telephonyManager =
(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);     
               // register PhoneStateListener
               PhoneStateListener callStateListener = new PhoneStateListener() {
                    public void onCallStateChanged(int state, String incomingNumber)
                    {
                            //  React to incoming call.
                            number=incomingNumber;

                             // If phone ringing
                            if(state==TelephonyManager.CALL_STATE_RINGING)
                            {
                                    Toast.makeText(getApplicationContext(),"Phone Is Riging", Toast.LENGTH_LONG).show();
                                   
                            }

                              // If incoming call received
                            if(state==TelephonyManager.CALL_STATE_OFFHOOK)
                            {
                                Toast.makeText(getApplicationContext(),"Phone is Currently in A call", Toast.LENGTH_LONG).show();
                            }
                           
                           
                            if(state==TelephonyManager.CALL_STATE_IDLE)
                            {
                                Toast.makeText(getApplicationContext(),"phone is neither ringing nor in a call", Toast.LENGTH_LONG).show();
                            }
                    }
                    };
                    telephonyManager.listen(callStateListener,PhoneStateListener.LISTEN_CALL_STATE);
                   
                   
                   
            }
                   

                   
            }



How To Launch the Dialer to Initiate Phone Calls:


Use an Intent action to start a dialer activity; you should specify the number to dial using the tel: schema as the data component of the Intent.

Use the Intent.ACTION_DIAL Activity action to launch a dialer rather than dial the number immediately.
This action starts a dialer Activity, passing in the specified number but allowing the dialer application
to manage the call initialization (the default dialer asks the user to explicitly initiate the call). This
action doesn’t require any permissions and is the standard way applications should initiate calls.

Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:1234567"));
startActivity(intent);










 

New Advance Topics:
Android ImageSwitcher                    Android TextSwitcher                                Android ViewFlipper
Android Gesture Detector               Handling/Detecting Swap Events                Gradient Drawable
Detecting Missed Calls                    Hide Title Bar                                           GridView Animation

 Beginning With Android
      Android : Introduction                                                              Configuring Eclipse for Android Development
     Creating Your First Android Project                                           Understanding Android Manifest File of your android app

 Advance Android Topics                                                              Customizing Android Views


Working With Layouts                                                                Working With Views

Understanding Layouts in Android                                                   Using Buttons and EditText in Android
Working with Linear Layout (With Example)                                     Using CheckBoxes in Android
Nested Linear Layout (With Example)                                              Using AutoCompleteTextView in Android                                                                                          Grid View
Relative Layout In Android                                                               ListView
Table Layout                                                                                   Android ProgressBar
Frame Layout(With Example)                                                          Customizing ProgressBar
Absolute Layout                                                                             Customizing Radio Buttons
Grid Layout                                                                                    Customizing Checkboxes In Android

Android Components                                                                 Dialogs In Android

Activity In Android                                                                    Working With Alert Dialog
Activity Life Cycle                                                                    Adding Radio Buttons In Dialog
Starting Activity For Result                                                       Adding Check Boxes In Dialog
Sending Data from One Activity to Other in Android                    Creating Customized Dialogs in Android
Returning Result from Activity                                                   Creating Dialog To Collect User Input
Android : Service                                                                     DatePicker and TimePickerDialog
BroadcastReceiver                                                                   Using TimePickerDialog and DatePickerDialog In android

Menus In Android                                                                ListView:
Creating Option Menu                                                               Populating ListView With DataBase
Creating Context Menu In Android                                              Populating ListView with ArrayList
                                                                                               ListView with Custom Adapter

Toast                                                                                      Working With SMS
Customizing Toast In Android                                                       How to Send SMS in Android
Customizing the Display Time of Toast                                        How To Receive SMS
Customizing Toast At Runtime                                                  Accessing Inbox In Android
Adding Image in Toast
Showing Toast for Longer Time


TelephonyManager                                                            Storage: Storing Data In Android
Using Telephony Manager In Android                                          SharedPreferences In Android
                                                                                              Reading and Writing files to Internal Stoarage
Working With Incoming Calls                                             DataBase
How To Handle Incoming Calls in Android                                Working With Database in Android
How to Forward an Incoming Call In Android                            Creating Table In Android
CALL States In Android                                                          Inserting, Deleting and Updating Records In Table in Android


Miscellaneous
Notifications In Android
How To Vibrate The Android Phone
Sending Email In Android
Opening a webpage In Browser
How to Access PhoneBook In Android
Prompt User Input with an AlertDialog