Question

I have created an android app, in that I wants last call detail of the number.

Using CallLog.Calls.DURATION I get the last call detail.

My code is-

    StringBuffer sb = new StringBuffer();
    Uri contacts = CallLog.Calls.CONTENT_URI;
    Cursor managedCursor = context.getContentResolver().query(contacts, 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 Details :");

    if(managedCursor.moveToFirst())
    {
        String phNumber = managedCursor.getString(number);
        String callType = managedCursor.getString(type);
        String callDate = managedCursor.getString(date);
        String callDayTime = new Date(Long.valueOf(callDate)).toString();
        // long timestamp = convertDateToTimestamp(callDayTime);
        String callDuration = managedCursor.getString(duration);
        int calld=Integer.parseInt(callDuration);

        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;
        }
        sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- " + dir + " \nCall Date:--- " + callDayTime + " \nCall duration in sec :--- " + calld);
        sb.append("\n----------------------------------");            

    }

    managedCursor.close();
    System.out.println(sb);

In one device I get last call detail using managedCursor.moveToFirst() and in another device I get last call detail using managedCursor.moveToLast().

How to achieve it using single code?

Was it helpful?

Solution

You can add an ORDER BY clause to the query to order them descending by the time of the call.

Cursor managedCursor = context.getContentResolver().query(contacts, null, null, null, CallLog.Calls.DEFAULT_SORT_ORDER);

OR

Cursor managedCursor = context.getContentResolver().query(contacts, null, null, null, CallLog.Calls.DATE + " DESC");

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top