Prabha Karan Prabha Karan - 1 year ago 55
SQL Question

How to get the Call Log data between two dates and Total number of incoming an outgoing calls between the dates

I want to get the call log history by today, yesterday, last seven days and last 30days along with that i want to show the total duration of incoming and outgoing calls of that particular number.
I tried with various type but none of them working. What should i do to get the output ?.I have no idea how to use the query to get the call log from specific date. Help me Thanks

public class CallLogActivity extends Activity {

TextView textView;

protected void onCreate(Bundle savedInstanceState) {
textView = (TextView) findViewById(;
// listcallLog = (ListView) findViewById(;



public void getCalldetails() {
StringBuffer stringBuffer = new StringBuffer();

String strOrder = android.provider.CallLog.Calls.DATE + " DESC";

Calendar calender = Calendar.getInstance();

calender.set(2016, calender.NOVEMBER, 18);
String fromDate = String.valueOf(calender.getTime());

calender.set(2016, Calendar.NOVEMBER, 20);
String toDate = String.valueOf(calender.getTimeInMillis());

String[] whereValue = {fromDate,toDate};

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
Cursor managedCursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, android.provider.CallLog.Calls.DATE + " BETWEEN ? AND ?", whereValue, strOrder);

// Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null, android.provider.CallLog.Calls.DATE, new String[]{" BETWEEN ? AND ?"}, strOrder);
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);

stringBuffer.append("Call Log :");

while (managedCursor.moveToNext())
String phoneNumber = managedCursor.getString(number);
String callType = managedCursor.getString(type);
String callDate = managedCursor.getString(date);
SimpleDateFormat formatter = new SimpleDateFormat(
"dd-MMM-yyyy HH:mm");
String dateString = formatter.format(new Date(Long
// 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";

case CallLog.Calls.INCOMING_TYPE:
dir = "INCOMMING";
case CallLog.Calls.MISSED_TYPE:
dir = "MISSED CALL";


stringBuffer.append("\nPhone Number:--- " + phoneNumber + "\nCall Type:--- "
+ dir + "\nCall Date:---"
+ dateString + "\nCall Duration:---" + callDuration);





Answer Source

go step by step . first get all call log history this way.

Uri allCalls = Uri.parse("content://call_log/calls");
Cursor c = managedQuery(allCalls, null, null, null, null);

String num= c.getString(c.getColumnIndex(CallLog.Calls.NUMBER));// for  number
String name= c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME));// for name
String duration = c.getString(c.getColumnIndex(CallLog.Calls.DURATION));// for duration
int type = Integer.parseInt(c.getString(c.getColumnIndex(CallLog.Calls.TYPE)));// for call type, Incoming or out going.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download