如何检测上次拨出呼叫的去电呼叫持续时间?

时间:2017-01-05 10:55:06

标签: java android

我完成了从通话记录中查看拨出电话详细信息的代码,我怎样才能获得最后一个拨出电话的持续时间。我尝试了很多次,但问题没有解决。在下面的代码中,我得到了拨出电话的详细信息,但所需的输出是获得 上次拨打电话的去电时间。请帮我解决这个问题......

  StringBuffer sb = new StringBuffer();
    Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null, null, null, CallLog.Calls.DATE + " DESC");
    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);
        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";
                sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- " + dir + " \nCall Date:--- " + callDayTime + " \nCall duration in sec :--- " + callDuration);
                sb.append("\n----------------------------------");
                textView.setText(sb);
                break;

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

                break;

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

    managedCursor.close();

1 个答案:

答案 0 :(得分:0)

这是逻辑,你需要存储传递的switch语句为true时的最后位置然后检索该位置并移动光标ant获取所需数据,如下面提到的更新代码中的代码

 StringBuffer sb = new StringBuffer();
            Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null, null, null, CallLog.Calls.DATE + " DESC");
            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 :");
            int currentCount =0, lastPosition =0;
            while (managedCursor.moveToNext()) {
                currentCount++;
                String phNumber = managedCursor.getString(number);
                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:
                        lastPosition = currentCount;
                        dir = "OUTGOING";
                        sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- " + dir + " \nCall Date:--- " + callDayTime + " \nCall duration in sec :--- " + callDuration);
                        sb.append("\n----------------------------------");
                        Log.e("test", sb.toString());
                        //textView.setText(sb);
                        break;

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

                        break;

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

            lastPosition--;
            managedCursor.moveToPosition(lastPosition);
            int requiredNumber = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
            String phNumber = managedCursor.getString(requiredNumber);
            Log.e("last position number ", phNumber);
            managedCursor.close();