Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the call log from specific date in android

In my app i am getting the all call log by using this code.it return me all the call log in my android phone.

public class CallLogHelper {

    public static Cursor getAllCallLogs(ContentResolver cr) {
        // reading all data in descending order according to DATE
        String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
        Uri callUri = Uri.parse("content://call_log/calls");
        Cursor curCallLogs = cr.query(callUri, null, null, null, strOrder);
        return curCallLogs;
    }

    public static void insertPlaceholderCall(ContentResolver contentResolver,
            String name, String number) {
        ContentValues values = new ContentValues();
        values.put(CallLog.Calls.NUMBER, number);
        values.put(CallLog.Calls.DATE, System.currentTimeMillis());
        values.put(CallLog.Calls.DURATION, 0);
        values.put(CallLog.Calls.TYPE, CallLog.Calls.OUTGOING_TYPE);
        values.put(CallLog.Calls.NEW, 1);
        values.put(CallLog.Calls.CACHED_NAME, name);
        values.put(CallLog.Calls.CACHED_NUMBER_TYPE, 0);
        values.put(CallLog.Calls.CACHED_NUMBER_LABEL, "");
        Log.d("Call Log", "Inserting call log placeholder for " + number);
        contentResolver.insert(CallLog.Calls.CONTENT_URI, values);
    }

}

But my problem is this i want to get the call log from specific date, not all call log. I have no idea how to use the query to get the call log from specific date. Help me Thanks

like image 515
Naveen Kumar Avatar asked Feb 02 '13 08:02

Naveen Kumar


2 Answers

StringBuffer sb = new StringBuffer();
    String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
    Uri callUri = Uri.parse("content://call_log/calls");

    Calendar calendar = Calendar.getInstance();

    calendar.set(2014, Calendar.MAY, 25);
    String fromDate = String.valueOf(calendar.getTimeInMillis());
    calendar.set(2014, Calendar.MAY, 30);
    String toDate = String.valueOf(calendar.getTimeInMillis());
    String[] whereValue = {fromDate,toDate};


    Cursor cur = cr.query(callUri, null, android.provider.CallLog.Calls.DATE+" BETWEEN ? AND ?", whereValue, strOrder);

    //Cursor cur = cr.query(callUri, null, android.provider.CallLog.Calls.DATE+" >= ?", whereValue, strOrder);
    // loop through cursor
    while (cur.moveToNext()) {
        String callNumber = cur.getString(cur
                .getColumnIndex(android.provider.CallLog.Calls.NUMBER));

        String callName = cur
                .getString(cur
                        .getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME));

        String callDate = cur.getString(cur
                .getColumnIndex(android.provider.CallLog.Calls.DATE));
        SimpleDateFormat formatter = new SimpleDateFormat(
                "dd-MMM-yyyy HH:mm");
        String dateString = formatter.format(new Date(Long
                .parseLong(callDate)));

        String callType = cur.getString(cur
                .getColumnIndex(android.provider.CallLog.Calls.TYPE));
        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; 
    } 

        String isCallNew = cur.getString(cur
                .getColumnIndex(android.provider.CallLog.Calls.NEW));

        String duration = cur.getString(cur
                .getColumnIndex(android.provider.CallLog.Calls.DURATION));

        sb.append("\nPhone Number:--- " + callNumber + " \nName:--- "+ callName +" \nCall Type dir:--- " + dir + " \nCall Date:--- " + dateString + " \n duration in sec :--- " + duration);
        sb.append("\n----------------------------------"); 

    }

The call log that returned is between 25/05/2014 & 30/05/2014. The returned value is all stored in sb variable, you can set this value in a TextView for testing

like image 187
Vattic Avatar answered Oct 14 '22 14:10

Vattic


        String[] strFields = {
                android.provider.CallLog.Calls.NUMBER, 
                android.provider.CallLog.Calls.TYPE,
                android.provider.CallLog.Calls.CACHED_NAME,
                android.provider.CallLog.Calls.DATE,
                android.provider.CallLog.Calls.DURATION,

                };

        // Defines a string to contain the selection clause
        String mSelectionClause = android.provider.CallLog.Calls.DATE+ " >= ?";

        // Initializes an array to contain selection arguments
        String[] mSelectionArgs = { createDate(2013,1,1).toString() };

        Cursor mCallCursor = currentContext.getContentResolver().query(
                android.provider.CallLog.Calls.CONTENT_URI,
                strFields,
                mSelectionClause,
                mSelectionArgs,
                null
                );

        return mCallCursor;

public static Long createDate(int year, int month, int day)
{
    Calendar calendar = Calendar.getInstance();

    calendar.set(year, month, day);

    return calendar.getTimeInMillis();

}
like image 39
Yaqub Ahmad Avatar answered Oct 14 '22 12:10

Yaqub Ahmad