如何在 Android Studio 中获取最新的通话记录?

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

我有一个代码可以在下面使用。但我不能限制它。我只想获取最近 20 条通话记录。 但这就是我如何看待所有时间的搜索日志。

应该只是最后的通话记录,我只需要看20条就可以了。任何帮助,我将不胜感激。

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();
        }
    }

在这里我们可以用我们已经创建的for循环创建一个整数,并在这里设置一个限制。

对不起我的英语不好

Function;

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 = 10; // intenger call log limit

        if (c.moveToLast()) { //starts pulling logs from last - you can use moveToFirst() for first logs
            for (int j = 0; j < totalCall; j++) {


                String phNumber = c.getString(c.getColumnIndexOrThrow(CallLog.Calls.NUMBER));
                String callDate = c.getString(c.getColumnIndexOrThrow(CallLog.Calls.DATE));
                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 CallLog.Calls.OUTGOING_TYPE:
                        direction = "OUTGOING";
                        break;
                    case CallLog.Calls.INCOMING_TYPE:
                        direction = "INCOMING";
                        break;
                    case CallLog.Calls.MISSED_TYPE:
                        direction = "MISSED";
                        break;
                    default:
                        break;
                }

                c.moveToPrevious(); // if you used moveToFirst() for first logs, you should this line to moveToNext

                Toast.makeText(getBaseContext(), phNumber + callDuration + callDayTimes + direction, Toast.LENGTH_SHORT).show(); // you can use strings in this line 

            }
        }
        c.close();
    }
}

请在清单文件中添加此权限。

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

在您的 Activity 文件中添加此函数并在 oncreate 函数中调用它。

 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();
}

要调用此函数,请将其添加到 Activity 中的 oncreate 函数。

getCallLogs();