我正在获得之前的通话时长而不是上次通话时长

时间:2017-07-19 06:31:29

标签: android calllog

我尝试了以下代码,其中呼叫持续时间不正确,之前的呼叫持续时间显示在当前的电话呼叫中。我尝试了代码并搜索了堆栈溢出,问题没有解决。

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

请帮我解决这个问题。如何获取android中的最后一个呼叫持续时间。

2 个答案:

答案 0 :(得分:1)

您需要在内容查询中使用limit子句来获取最后的呼叫详细信息。因此,您的内容查询将成为

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

答案 1 :(得分:0)

试试这段代码:

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