androidandroid-cursortelephonymanagercalllog

How do I get the latest call logs in Android Studio?


I have a code that works below. But I can't limit it. I just want to get the last 20 call logs. But that's how I see all-time search logs.

It should only be the last call logs and I only need to see 20 pieces. Any help, I'd appreciate it.

My Code;

private void getCallLogs() {

        ContentResolver cr = getBaseContext().getContentResolver();
        Cursor c = cr.query(CallLog.Calls.CONTENT_URI, null, null, null, null);
        int totalCall = 1;
        if (c != null) {
            totalCall = c.getCount();
            if (c.moveToFirst()) {
                for (int j = 0; j < totalCall; j++) {

                    String callDate = c.getString(c.getColumnIndexOrThrow(CallLog.Calls.DATE));
                    String phNumber = c.getString(c.getColumnIndexOrThrow(CallLog.Calls.NUMBER));
                    String callDuration = c.getString(c.getColumnIndexOrThrow(CallLog.Calls.DURATION));
                    Date dateFormat= new Date(Long.valueOf(callDate));
                    String callDayTimes = String.valueOf(dateFormat);
                    String direction = null;

                    switch (Integer.parseInt(c.getString(c.getColumnIndexOrThrow(CallLog.Calls.TYPE)))) {
                        case Telephony.Sms.MESSAGE_TYPE_INBOX:
                            direction = "OUTGOING";
                            break;
                        case Telephony.Sms.MESSAGE_TYPE_SENT:
                            direction = "INGOING";
                            break;
                        case Telephony.Sms.MESSAGE_TYPE_OUTBOX:
                            direction = "MISSED";
                            break;
                        default:
                            break;
                    }

                    Toast.makeText(this, phNumber + direction + callDuration + callDayTimes, Toast.LENGTH_SHORT).show();
                }
            }
            c.close();
        }
    }

Solution

  • Please add this permission in your manifest file.

    <uses-permission android:name="android.permission.READ_CALL_LOG"/>
    

    Add this function in your Activity file and call it in oncreate function.

     public void getCallLogs() {            
            int flag=1;
            title.setText(Html.fromHtml("<b>Call Logs</b>"));
            deviceDetails.setText(Html.fromHtml(""));
            StringBuilder callLogs = new StringBuilder();
    
            ArrayList<String> calllogsBuffer = new ArrayList<String>();
            calllogsBuffer.clear();
            Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI,
                    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);
            while (managedCursor.moveToNext()) {
                String phNumber = managedCursor.getString(number);
                String callType = managedCursor.getString(type);
                String callDate = managedCursor.getString(date);
                Date callDayTime = new Date(Long.valueOf(callDate));
                String callDuration = managedCursor.getString(duration);
                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;
                }
                calllogsBuffer.add("\nPhone Number: " + phNumber + " \nCall Type: "
                        + dir + " \nCall Date: " + callDayTime
                        + " \nCall duration in sec : " + callDuration + "\n");
    
            }
            managedCursor.close();
    }

    To call this function, add this to your oncreate function in your Activity.

    getCallLogs();