user10473409
user10473409

Reputation:

Android: How to search through call logs for a specific number

I have function that collects all call logs, Incoming, outgoing and missed. It also gives me the call duration, date, type and time.

I want to search through this Call log for a specific number, "12345678".

Function:

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

Just wondering how I would do this?

Appreciate any suggestions or answers!

Upvotes: 2

Views: 2653

Answers (1)

user10473409
user10473409

Reputation:

After lot of work, I finally found the solution. This code worked for me.

@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);


                }

            }

    }

Hope this helps others!

Upvotes: 1

Related Questions