Saturday, 26 October 2013

Read Incoming,Outgoing and Missed call Log History programmatically



                   Read Incoming,Outgoing and Missed call Log History programmatically 


// Missed Call Reading


 private void getCallDetails() {

 StringBuffer sb = new StringBuffer();
 Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null,
   null, null, null);
 int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
 int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
 int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
 int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
 sb.append("Call Log :");
 while (managedCursor.moveToNext()) {
  String phNumber = managedCursor.getString(number);
  String callType = managedCursor.getString(type);
  String callDate = managedCursor.getString(date);
  Date callDayTime = new Date(Long.valueOf(callDate));
  String callDuration = managedCursor.getString(duration);
  String dir = null;
  int dircode = Integer.parseInt(callType);
  switch (dircode) {
//   case CallLog.Calls.OUTGOING_TYPE:
//    dir = "OUTGOING";
//    break;
//
//   case CallLog.Calls.INCOMING_TYPE:
//    dir = "INCOMING";
//    break;
  case CallLog.Calls.MISSED_TYPE:
   dir = "MISSED";
   break;
  }


  if(dir!=null)
  {
  sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- "
    + dir + " \nCall Date:--- " + callDayTime
    + " \nCall duration in sec :--- " + callDuration);
  sb.append("\n----------------------------------");

  }



 }
 //managedCursor.close();
// textView.setText(sb);

 Log.e("Result for Missed calls","==========="+sb);


}

//Outgoing call Reading

 private void getOutgoingCallDetails() {

 StringBuffer sb = new StringBuffer();
 Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null,
   null, null, null);
 int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
 int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
 int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
 int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
 sb.append("Call Log :");
 while (managedCursor.moveToNext()) {
  String phNumber = managedCursor.getString(number);
  String callType = managedCursor.getString(type);
  String callDate = managedCursor.getString(date);
  Date callDayTime = new Date(Long.valueOf(callDate));
  String callDuration = managedCursor.getString(duration);
  String dir = null;
  int dircode = Integer.parseInt(callType);
  switch (dircode) {
  case CallLog.Calls.OUTGOING_TYPE:
   dir = "OUTGOING";
   break;
//
//   case CallLog.Calls.INCOMING_TYPE:
//    dir = "INCOMING";
//    break;
//   case CallLog.Calls.MISSED_TYPE:
//    dir = "MISSED";
//    break;
  }
  if(dir!=null)
  {
  sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- "
    + dir + " \nCall Date:--- " + callDayTime
    + " \nCall duration in sec :--- " + callDuration);
  sb.append("\n----------------------------------");

  }
 }
 //managedCursor.close();
// textView.setText(sb);

 Log.e("Result for Missed calls","==========="+sb);


}


//Incoming call Reading

 private void getIncomingCallDetails() {

 StringBuffer sb = new StringBuffer();
 Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null,
   null, null, null);
 int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
 int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
 int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
 int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
 sb.append("Call Log :");
 while (managedCursor.moveToNext()) {
  String phNumber = managedCursor.getString(number);
  String callType = managedCursor.getString(type);
  String callDate = managedCursor.getString(date);
  Date callDayTime = new Date(Long.valueOf(callDate));
  String callDuration = managedCursor.getString(duration);
  String dir = null;
  int dircode = Integer.parseInt(callType);
  switch (dircode) {
//   case CallLog.Calls.OUTGOING_TYPE:
//    dir = "OUTGOING";
//    break;
//
  case CallLog.Calls.INCOMING_TYPE:
   dir = "INCOMING";
   break;
//   case CallLog.Calls.MISSED_TYPE:
//    dir = "MISSED";
//    break;
  }
  if(dir!=null)
  {
  sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- "
    + dir + " \nCall Date:--- " + callDayTime
    + " \nCall duration in sec :--- " + callDuration);
  sb.append("\n----------------------------------");

  }
 }
 //managedCursor.close();
// textView.setText(sb);

 Log.e("Result for Missed calls","==========="+sb);


}


To get call history programmatically first add read conact permission in Manifest file :
<uses-permission android:name="android.permission.READ_CONTACTS" />