Mihir Shah
Mihir Shah

Reputation: 996

Filter call logs for some given time slot in a day

I want to get call logs for some given time slot in android. For getting call logs, I use the following code mCursor=getContentResolver().query(CallLog.Calls.CONTENT_URI,PROJECTION,mSelection,mSelectionArgs, null); and here I replaced mSelection and mSelectionArgs by null for getting all the call logs.

Now, suppose I want just the call logs from 9 to 11 in the morning, is it possible? If so, what should I put in mSelection and mSelectionArgs? Or, any other way to filter the call logs, instead of manually checking every row from mCursor using some iteration and then filter it.

Upvotes: 0

Views: 1473

Answers (1)

skynet
skynet

Reputation: 9908

You can just filter the results yourself based on your date range.

int date = mCursor.getColumnIndex(CallLog.Calls.DATE);

while (mCursor.moveToNext()) {
    String callDate = mCursor.getString(date);
    Date callDayTime = new Date(Long.valueOf(callDate));

    // filter by date

If your filter criteria is simple enough (say, you want calls between two times) you can form a query that only returns dates in that given range. For instance, to get all calls from noon to 7pm on October 12 of this year:

Calendar cal = Calendar.getInstance();
cal.set(2013, Calendar.OCTOBER, 12, 12, 0, 0);
Date afterDate = cal.getTime();
cal.set(2013, Calendar.OCTOBER, 12, 19, 0, 0);
Date beforeDate = cal.getTime();

final String SELECT = CallLog.Calls.DATE + ">?" + " AND "
        + CallLog.Calls.DATE + "<?";

Cursor managedCursor = getContentResolver().query(
        CallLog.Calls.CONTENT_URI,
        new String[] { CallLog.Calls.DATE, CallLog.Calls.DURATION,
                CallLog.Calls.TYPE, CallLog.Calls.NUMBER,
                CallLog.Calls._ID },
        SELECT,
        new String[] { String.valueOf(afterDate.getTime()),
                String.valueOf(beforeDate.getTime()) },
        CallLog.Calls.DATE + " desc");

If you want to filter for calls between noon and 7pm regardless of day, I think the first loop is your best bet (you will either need to use a large SELECT clause or figure out how to get the hour from a long value in SQL.

Upvotes: 2

Related Questions