broadcast receiver for missed call in android

10,705

Solution 1

There is no specific broadcast for a missed call, AFAIK.

You can watch for ACTION_PHONE_STATE_CHANGED broadcasts, wait until the phone shifts from EXTRA_STATE_RINGING to EXTRA_STATE_IDLE, then try checking the CallLog content provider to see if the call was missed. I have not tried this technique, but it may work.

Solution 2

You need to use a ContentObserver

public class MissedCallsContentObserver extends ContentObserver
{
    public MissedCallsContentObserver()
    {
        super(null);
    }

    @Override
    public void onChange(boolean selfChange)
    {
        Cursor cursor = getContentResolver().query(
            Calls.CONTENT_URI, 
            null, 
            Calls.TYPE +  " = ? AND " + Calls.NEW + " = ?", 
            new String[] { Integer.toString(Calls.MISSED_TYPE), "1" }, 
            Calls.DATE + " DESC ");

        //this is the number of missed calls
        //for your case you may need to track this number
        //so that you can figure out when it changes
        cursor.getCount(); 

        cursor.close();
    }
}

From your app, you just need to do this:

MissedCallsContentObserver mcco = new MissedCallsContentObserver();
getApplicationContext().getContentResolver().registerContentObserver(Calls.CONTENT_URI, true, mcco);
Share:
10,705
Sujit
Author by

Sujit

An android enthusiastic and passionate about coding. Started working on android since its evolving stage. just love Android. "Good code is its own best documentation.”

Updated on June 25, 2022

Comments

  • Sujit
    Sujit almost 2 years

    Does anyone know what is the intent for missed call. Actually i want to send sms on missed call and incomming call in my application.

  • Name is Nilay
    Name is Nilay about 11 years
    @CommonsWare-Can we differentiate between Missed Call and Rejected Call because both function in the above mentioned way !!!
  • lock
    lock over 10 years
    Hi, i would like to know if you intentionally left out cursor.close()? Like it is not necessary because the ContentObserver will close it automatically?
  • Joe
    Joe over 10 years
    good question. I did not leave it out intentionally, it should be there. I will update my answer.