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>