Android:如何在通话记录中搜索特定号码

时间:2018-11-12 18:46:42

标签: java android

我具有收集所有呼叫日志(传入,传出和未接)的功能。它还给了我通话时间,日期,类型和时间。

我想在此通话记录中搜索特定的号码“ 12345678”。

功能:

    StringBuffer sb = new StringBuffer();
    Cursor managedCursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);
    int snumber = 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 Log :");
    String phNumber = null;
    while (managedCursor.moveToNext()) {
        phNumber = managedCursor.getString(snumber);
        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;

        }

        sb.append("\nPhone Number:--- " + phNumber +
                " \nCall Type:--- " + dir +
                " \nCall Date:--- " + callDayTime +
                " \nCall duration in sec :--- " +
                callDuration);
        sb.append("\n----------------------------------");


    }
    System.out.println(phNumber);

只是想知道我会怎么做?

感谢任何建议或答案!

1 个答案:

答案 0 :(得分:1)

经过大量工作,我终于找到了解决方案。这段代码对我有用。

@Override
public void onCreate() {




    String number = “12345678”


    StringBuffer sb = new StringBuffer();
    Cursor managedCursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);
    int snumber = 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);
    List<String> callList = new ArrayList<String>();


    while (managedCursor.moveToNext()) {
        callList.add(number);
        String callDate = managedCursor.getString(date);
        callList.add(callDate);
        String callDuration = managedCursor.getString(duration);
        Date callDayTime = new Date(Long.valueOf(callDate));


        break;
    }
    String phNumber = number;
    callList.add(phNumber);
    String callType = managedCursor.getString(type);
    callList.add(callType);


            if (callList.contains(phNumber)) {

                    System.out.println("The phone number is " + phNumber );


                } else {

                    System.out.println(“Couldn’t find number ” + phNumber);


                }

            }

    }

希望这对其他人有帮助!