Domanda

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?

È stato utile?

Soluzione

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");

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top