如何追踪上次通话时长?

时间:2017-06-27 04:50:09

标签: android phone-state-listener

我在通话记录项目中工作,因为我可以跟踪来电和去电,但跟踪上次通话时间不准确。当我跟踪上次通话时间时,前一次通话的持续时间会显示给当前通话。

最初我试图跟踪拨出电话的持续时间,但是无法通过使用电话状态监听器跟踪拨出电话,因此我在默认电话呼叫日志中获取了最后一个呼叫时长。

我搜索了堆栈溢出,每个人都发布了这个答案,但没有人发布正确答案来跟踪最后一个拨出电话的持续时间。

这是我的代码

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

在上面的代码中,我可以跟踪上次通话时间。

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

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