getting the call logs of incoming and outgoing calls in android programmatically

前端 未结 5 1849
执笔经年
执笔经年 2021-02-02 04:34

I am making an app in which I want to get the call logs of all incoming, outgoing and missed calls. How can I do that?

5条回答
  •  一整个雨季
    2021-02-02 05:18

    Yup. its works me:) try this.

    private void getCallDetails() {
    
        StringBuffer sb = new StringBuffer();
        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);
        sb.append("Call Details :");
        while (managedCursor.moveToNext()) {
            String phNumber = managedCursor.getString(number); // mobile number
            String callType = managedCursor.getString(type); // call type
            String callDate = managedCursor.getString(date); // call 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----------------------------------");
        }
        managedCursor.close();
        miss_cal.setText(sb);
        Log.e("Agil value --- ", sb.toString());
    }
    

    Note :

    1. You want to get the particular call type ? then use the below code.

    2. For eg :- if i want income call alone then command/remove the same code in the switch case beneath

    3. Then use the below code inside income call case.

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

提交回复
热议问题