Android Call Log change notification example

Learnt about How to read call logs in Android phone, this tutorial will tell how to listen to Android call log changes in  phone.

If any call log entry is added, deleted or updated, then fire a notification event. Here are the simple steps.

1) Create a class to receive the call log change events

public class CallLogChangeObserverClass extends ContentObserver
{
public CallLogChangeObserverClass(Handler handler, Context ct) {
super(handler);
// TODO Auto-generated constructor stub
}

public void onChange(boolean selfChange) {
Toast.makeText(getApplicationContext(), "call log changed", Toast.LENGTH_LONG).show();
//Read call Logs here
}
}

2) Register  & unregister for content observer from your activity for call log change events.

CallLogChangeObserverClass callLogchangeevents = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

callLogsReader = (Button)findViewById(R.id.ID_READ_CALL_LOGS_BTN);

callLogchangeevents = new CallLogChangeObserverClass(new Handler(),this);

//Registering content observer
getContentResolver().registerContentObserver(CallLog.Calls.CONTENT_URI, true,
callLogchangeevents);

}

Now give a missed call and test it. Make sure you unregister once you finish your work, or upon exist from activity. Else this would cause memory leak.

//Unregister from call log change activities
getapplicationcontext().getContentResolver().unregisterContentObserver(callLogchangeevents);