2

I am working in call log project ,in that i can track the incoming and outgoing calls but tracking for last call duration is not accurate.When i track the last call duration the duration of previous call is shown to the current calls.

Initially i tried to track the duration for outgoing calls but it is not possible to track the outgoing calls by using the phone state listener so i fetched the last call duration in default phone call log.

I searched for stack overflow every one posted this answer but no one posted correct answer to track the last outgoing call duration.

This is my code

 private int outgoingCallDuration(Context context) {

    int sum = 0;
    StringBuffer sb = new StringBuffer();


    try {
        //
        Cursor managedCursor = context.getContentResolver().query(android.provider.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 :");
        Log.e("total count", "" + managedCursor.getCount());
        //managedCursor.moveToPosition(managedCursor.getCount() - 1);
        int currentCount = 0, lastPosition = 0;
        while (managedCursor.moveToNext()) {
            currentCount++;
            //managedCursor.moveToPosition(managedCursor.getCount() - 1);
            String phNumber = managedCursor.getString(number);
            String callType = managedCursor.getString(type);
            String callDate = managedCursor.getString(date);
            java.sql.Date callDayTime = new java.sql.Date(Long.valueOf(callDate));
            String callDurations = managedCursor.getString(duration);
            String dir = null;
            int dircode = Integer.parseInt(callType);


            switch (dircode) {

                case CallLog.Calls.OUTGOING_TYPE:
                    //  lastPosition = currentCount;
                    dir = "OUTGOING";
                    break;

                case CallLog.Calls.INCOMING_TYPE:
                    dir = "INCOMING";
                    break;

                case CallLog.Calls.MISSED_TYPE:
                    dir = "MISSED";
                    break;

            }
            lastPosition = currentCount;

        }

        lastPosition--;
        managedCursor.moveToLast();
        int requiredNumber = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
        int durations = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
        String phNumber = managedCursor.getString(requiredNumber);
        String dur = managedCursor.getString(durations);

        //  Long durat = Long.parseLong(dur);
        int myNum = Integer.parseInt(dur);
        managedCursor.close();
        Log.e("last position number ", phNumber);
        Log.e("last position duration ", dur);

        return myNum;
    } catch (SecurityException ex) {
        Log.d("CallReceiver", "outgoingCallDuration: Permission to read call is not allowed by user!");
        return 0;
    }
}

In the above code i can track the last call duration.

Prabha Karan
  • 1,309
  • 3
  • 17
  • 35
  • My code for deecting calls at https://stackoverflow.com/questions/15563921/how-to-detect-incoming-calls-in-an-android-device/15564021#15564021 should provide the duration as well. – Gabe Sechan Jun 27 '17 at 04:52
  • @Gabe Sechan in your answer , only outgoing and incoming call states can be tracked but in need to track the lst call duration. – Prabha Karan Jun 27 '17 at 04:58
  • And the last call is either an outgoing or an incoming call. You keep track of whichever one is called last. Its a trivial layer on top. – Gabe Sechan Jun 27 '17 at 04:59
  • @Gabe Sechan outgoing call starts in TelephonyManager.CALL_STATE_OFFHOOK and ends in TelephonyManager.CALL_STATE_IDLE , the issue in this is the outgoing call duration is tracked with outgoing ringing – Prabha Karan Jun 27 '17 at 05:06
  • In that i don't want to track the duration with ringing, i want to track the outgoing call duration when the call is attended. – Prabha Karan Jun 27 '17 at 05:07

1 Answers1

0

you can try this one out:

  public String LastCall()
    {
        StringBuffer sb = new StringBuffer();
        Cursor cur = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, android.provider.CallLog.Calls.DATE + " DESC");

        int number = cur.getColumnIndex(CallLog.Calls.NUMBER);
        int duration = cur.getColumnIndex(CallLog.Calls.DURATION);
        sb.append("Call Details : \n");
        while (cur.moveToNext())
        {
            String phNumber = cur.getString(number);
            String callDuration = cur.getString(duration);
            sb.append("\nPhone Number:" + phNumber);
            break;
        }
        cur.close();
        String str = sb.toString();
        return str;
    }