Detect Missed Call in Android with example


         Detecting Missed call is quite a simple process in Android, we need to utilize Android Telephony manager for this concept. If you require a brief overview about Android Telephony manager go to this tutorial.  
         There are three important constants in Android Telephony manager, that will help us to detect whether the call received was a missed call. Let me give a short information about the constants :
(If you are comfortable to read and understand, just see the image below, that will clearly say what these states mean actually)

CALL_STATE_IDLE            - No activity in phone related to phone calls.

CALL_STATE_OFFHOOK  - When you have at-least one call in active mode or Call in hold

CALL_STATE_RINGING     - A new call arrived and is ringing or waiting.

Telephony Manager Constants

To programmatically decide whether its a missed lets declare declare two static variables as shown below:
static boolean ring = false;
static boolean callReceived = false;

callReceived will be set to True when some call is actually received (CALL_STATE_OFFHOOK). ring will be set to True when the phone is ringing (CALL_STATE_RINGING).
With these conditions set, when the call is in idle state (CALL_STATE_IDLE) we can detect missed call with the following condition:
if (ring == true && callReceived == false)
{
//Then its a missed call
}

This condition means, Phone ringed but it was not picked and hence its a missed call.
Hope this will help you to better understand how to detect missed call in Android programmatically, might also help you to develop android app to detect missed call.

Android Permission for detecting Missed call :
               Finally add permissions to read phone state to AndroidManifest.xml
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

MainActivity.java


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity {

 static boolean ring = false;
 static boolean callReceived = false;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  TelephonyManager TelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
  TelephonyMgr.listen(new TeleListener(),
    PhoneStateListener.LISTEN_CALL_STATE);
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

 class TeleListener extends PhoneStateListener {
  public void onCallStateChanged(int state, String incomingNumber) {
   super.onCallStateChanged(state, incomingNumber);
   switch (state) {

   case TelephonyManager.CALL_STATE_IDLE:

    if (ring == true && callReceived == false) {
     Toast.makeText(getApplicationContext(),
       "Missed call from : " + incomingNumber,
       Toast.LENGTH_LONG).show();
    }
    // CALL_STATE_IDLE;
    Toast.makeText(getApplicationContext(), "CALL_STATE_IDLE",
      Toast.LENGTH_LONG).show();
    break;

   case TelephonyManager.CALL_STATE_OFFHOOK:
    // CALL_STATE_OFFHOOK;
    callReceived = true;
    Toast.makeText(getApplicationContext(), "CALL_STATE_OFFHOOK",
      Toast.LENGTH_LONG).show();
    break;

   case TelephonyManager.CALL_STATE_RINGING:
    ring = true;
    // CALL_STATE_RINGING
    Toast.makeText(getApplicationContext(), incomingNumber,
      Toast.LENGTH_LONG).show();
    Toast.makeText(getApplicationContext(), "CALL_STATE_RINGING",
      Toast.LENGTH_LONG).show();
    break;

   default:
    break;
   }
  }

 }

}

AndroidManifest.xml


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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <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" >
        <activity
            android:name="com.example.missedcallandroid.MainActivity"
            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>

Source code of this Application
DetectMissedCall_Android.zip




Reactions:

2 comments :

  1. Thanks, it works. I searched a lot and finally I found..

    ReplyDelete
  2. What if the user forcefully disconnects the call ? Then also it will be counted as a missed call.

    ReplyDelete