Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 594
Mehul Kanzariya Avatar asked Mar 09 '23 18:03

Mehul Kanzariya


1 Answers

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;

    }
like image 166
ak sacha Avatar answered Mar 28 '23 02:03

ak sacha