androidcalllog

Get last call details from Call Log


I am using below code to get last call details from call log.

public static CallDetails getLastCallDetails(Context context) {

    CallDetails callDetails = new CallDetails();

    Uri contacts = CallLog.Calls.CONTENT_URI;
    try {

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

        int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
        int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
        int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
        int incomingtype = managedCursor.getColumnIndex(String.valueOf(CallLog.Calls.INCOMING_TYPE));


        while (managedCursor.moveToNext()) {
            String callType;
            String phNumber = managedCursor.getString(number);
            String callerName = getContactName(context, phNumber);
            if(incomingtype == -1){
                callType = "incoming";
            }
            else {
                callType = "outgoing";
            }
            String callDate = managedCursor.getString(date);
            String callDayTime = new      Date(Long.valueOf(callDate)).toString();

            String callDuration = managedCursor.getString(duration);

            callDetails.setCallerName(callerName);
            callDetails.setCallerNumber(phNumber);
            callDetails.setCallDuration(callDuration);
            callDetails.setCallType(callType);
            callDetails.setCallTimeStamp(callDayTime);

        }
        managedCursor.close();

    } catch (SecurityException e) {
        Log.e("Security Exception", "User denied call log permission");

    }

    return callDetails;

}

The problem is that it returns the last second call and not the last call. I need to return the last call. I googled it but I am not able to get the perfect solution. Please help. Thanks in advance.


Solution

  • Add this line managedCursor.moveToFirst()

     public static CallDetails getLastCallDetails(Context context) {
    
            CallDetails callDetails = new CallDetails();
    
            Uri contacts = CallLog.Calls.CONTENT_URI;
            try {
    
                Cursor managedCursor = context.getContentResolver().query(contacts, null, null, null, android.provider.CallLog.Calls.DATE + " DESC limit 1;");
    
                int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
                int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
                int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
                int incomingtype = managedCursor.getColumnIndex(String.valueOf(CallLog.Calls.INCOMING_TYPE));
    
               if(managedCursor.moveToFirst()){ // added line
    
                while (managedCursor.moveToNext()) { 
                    String callType;
                    String phNumber = managedCursor.getString(number);
                    String callerName = getContactName(context, phNumber);
                    if(incomingtype == -1){
                        callType = "incoming";
                    }
                    else {
                        callType = "outgoing";
                    }
                    String callDate = managedCursor.getString(date);
                    String callDayTime = new      Date(Long.valueOf(callDate)).toString();
    
                    String callDuration = managedCursor.getString(duration);
    
                    callDetails.setCallerName(callerName);
                    callDetails.setCallerNumber(phNumber);
                    callDetails.setCallDuration(callDuration);
                    callDetails.setCallType(callType);
                    callDetails.setCallTimeStamp(callDayTime);
    
              }
            }
                managedCursor.close();
    
            } catch (SecurityException e) {
                Log.e("Security Exception", "User denied call log permission");
    
            }
    
            return callDetails;
    
        }