Monday, June 24, 2013

Detect Missed Call In Android

How to detect a Missed Call in Android



Prerequisite:You should know how to handle Incoming Call. if not, read the post  Handle Incomig Calls using BroadCastReceiver

How to approach :
whenever there as an incoming Phone goes in "RINGING" state, and if the person has not picked/received the call Phone Rings till a particular time and then goes in "IDLE" state.   So we have to monitor the Phone State and check the State accordingly.

Permission Required: 

Following permission is required to listen/monitor the Phone State.
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Declare the above permission in manifest file.

Listening Phone State:


We use a BroadcastReceiver class that will monitor the Phone State, whenever there is a change in Phone State, the onReceive() method of BroadcasrReceiver will be called, and in onReceivee(),  we check for the condition, See the Code below

do not forget to register the BroadcastReceiver in manifest  (see the manifest file at the bottom )


IncommingCallReceiver.java


public class IncommingCallReceiver extends BroadcastReceiver
 {
   
      static boolean ring=false;
      static boolean callReceived=false;
     
     
      @Override
      public void onReceive(Context mContext, Intent intent)
      {
         
             // Get the current Phone State
            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
           
            if(state==null)
                return;

            // If phone state "Rininging"
            if(state.equals(TelephonyManager.EXTRA_STATE_RINGING))
            {           
                      ring =true;
                     // Get the Caller's Phone Number
                     Bundle bundle = intent.getExtras();
                     callerPhoneNumber= bundle.getString("incoming_number");
              }



             // If incoming call is received
            if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
             {
                    callReceived=true;
              }


             // If phone is Idle
            if (state.equals(TelephonyManager.EXTRA_STATE_IDLE))
            {
                      // If phone was ringing(ring=true) and not received(callReceived=false) , then it is a missed call
                       if(ring==true&&callReceived==false)
                       {
                                Toast.makeText(mContext, "It was A MISSED CALL from : "+callerPhoneNumber, Toast.LENGTH_LONG).show();
                       }
          }
       
}






 Your manifest should look like follows

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />
   
       <!--  declare the permission -->
        <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" >
              
        <!-- Register the Broadcast receiver  -->
        <receiver android:name=".IncommingCallReceiver" android:enabled="true">
            <intent-filter>
                    <action android:name="android.intent.action.PHONE_STATE" />
                </intent-filter>
           </receiver>
    </application>

</manifest>






16 comments:

  1. Its simple and clean example. Thanks!

    ReplyDelete
  2. Thanks, it works great. But I need also unanswered outgoing calls. Is it possible to detect it via BroadcastRecevier and how?

    ReplyDelete
  3. Hi, good example. But you need to reset the vars during ringing and after missed call for the next call.

    ReplyDelete
  4. this code not woking on my mobile...is their any settings.?

    ReplyDelete
  5. what should i pass in the Intent parameter when i call this function in my activity...please help

    ReplyDelete
    Replies
    1. No need to pass anything, onReceive() will be called itself with the intent.

      Delete
    2. hi, there is nothing happen with me.while i used above code.
      its giving....... No Launcher activity found! & The launch will only sync the application package on the device!
      in console.what to do

      Delete
  6. what should i pass intent parameter while calling this function in activity

    ReplyDelete
  7. If i wanted more data about the incoming call ... say if nothing was in contacts for a incoming call ... where would I look to see what else came with intent.getExtras(); into our bundle?

    ReplyDelete
  8. thank you it's nice n very useful example

    ReplyDelete
  9. Your code is correct but you should use this snippest for idle state then it will give perfect answer

    // phone is idle
    if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
    // detect missed call
    if (isRinging == true && isReceived == false) {
    Toast.makeText(mContext,
    “Got a missed call from : ” + callerPhoneNumber,
    Toast.LENGTH_LONG).show();

    Toast.makeText(mContext, “Missed”, Toast.LENGTH_LONG).show();
    }

    isRinging = false;
    isReceived = false;
    }
    }






    you need to false boolean variable in idle state condition else it will cause a bug

    BUG–>if i got missed call then 2nd if i receve d call then 3rd if i missed d call then this scenario will not able to capture thru ur code so use my above snippest in ur code then it will work fyne in all scenario

    ReplyDelete
  10. this is work fine for my app but not working whenever application is not running any solution for that???

    ReplyDelete
  11. nothing is happening here.And it is showing
    No Launcher activity found!
    The launch will only sync the application package on the device!

    what to do

    ReplyDelete
  12. hi solveed that problem can i get name if the no is saved in my contact list

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
  14. Messages would then be able to be sent automatically by means of email, enabling organizations to oversee and get messages while far from the workplace. https://callgear.com/product/call-recording/

    ReplyDelete