Prabha Karan
Prabha Karan

Reputation: 1319

I am getting previous call duration instead of last call duration

I tried the below code in that ,the call duration getting is not correct, the previous call duration is shown to the current phone call. I tried the code and searched over the stack overflow ,problem is not solved.

  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();
            managedCursor.moveToPosition(lastPosition);
            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;
        }
    }

Please help me how to solve this. How to get the last call duration in android.

Upvotes: 0

Views: 905

Answers (2)

M.Waqas Pervez
M.Waqas Pervez

Reputation: 2430

Try this code :

 public double getLastOutgoingCallDuration(Context context) {
    Cursor c = context.getContentResolver()
            .query(CallLog.Calls.CONTENT_URI, null, null, null, null);
    double callDuration = 0;

    if (c != null) {
        while (c.moveToNext()) {
            int type = Integer.parseInt(c.getString(c.getColumnIndex(CallLog.Calls.TYPE)));
            String duration = c.getString(c.getColumnIndex(CallLog.Calls.DURATION));
            if (type == CallLog.Calls.OUTGOING_TYPE)
                callDuration = Double.parseDouble(duration);
            c.close();
        }
    }
    return callDuration;
}

Upvotes: 0

akhilesh0707
akhilesh0707

Reputation: 6899

You need to use limit clause in your content query to get the last call details. So your content query will become

Cursor cur = getContentResolver().query( CallLog.Calls.CONTENT_URI,null, null,null, android.provider.CallLog.Calls.DATE + " DESC limit 1;");

Upvotes: 1

Related Questions