从通话记录中获取最近通话的详细信息 [英] Get last call details from Call Log

查看:109
本文介绍了从通话记录中获取最近通话的详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码从通话记录中获取上次通话的详细信息。

I am using below code to get last call details from call log.

public static CallDetails getLastCallDetails(Context context) {

    CallDetails callDetails = new CallDetails();

    Uri contacts = CallLog.Calls.CONTENT_URI;
    try {

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

        int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
        int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
        int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
        int incomingtype = managedCursor.getColumnIndex(String.valueOf(CallLog.Calls.INCOMING_TYPE));


        while (managedCursor.moveToNext()) {
            String callType;
            String phNumber = managedCursor.getString(number);
            String callerName = getContactName(context, phNumber);
            if(incomingtype == -1){
                callType = "incoming";
            }
            else {
                callType = "outgoing";
            }
            String callDate = managedCursor.getString(date);
            String callDayTime = new      Date(Long.valueOf(callDate)).toString();

            String callDuration = managedCursor.getString(duration);

            callDetails.setCallerName(callerName);
            callDetails.setCallerNumber(phNumber);
            callDetails.setCallDuration(callDuration);
            callDetails.setCallType(callType);
            callDetails.setCallTimeStamp(callDayTime);

        }
        managedCursor.close();

    } catch (SecurityException e) {
        Log.e("Security Exception", "User denied call log permission");

    }

    return callDetails;

}

问题是它返回了最后一个第二个调用而不是最后一个电话。我需要返回上一个电话。我用谷歌搜索,但无法获得完美的解决方案。请帮忙。

The problem is that it returns the last second call and not the last call. I need to return the last call. I googled it but I am not able to get the perfect solution. Please help. Thanks in advance.

推荐答案

添加此行 managedCursor.moveToFirst()

 public static CallDetails getLastCallDetails(Context context) {

        CallDetails callDetails = new CallDetails();

        Uri contacts = CallLog.Calls.CONTENT_URI;
        try {

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

            int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
            int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
            int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
            int incomingtype = managedCursor.getColumnIndex(String.valueOf(CallLog.Calls.INCOMING_TYPE));

           if(managedCursor.moveToFirst()){ // added line

            while (managedCursor.moveToNext()) { 
                String callType;
                String phNumber = managedCursor.getString(number);
                String callerName = getContactName(context, phNumber);
                if(incomingtype == -1){
                    callType = "incoming";
                }
                else {
                    callType = "outgoing";
                }
                String callDate = managedCursor.getString(date);
                String callDayTime = new      Date(Long.valueOf(callDate)).toString();

                String callDuration = managedCursor.getString(duration);

                callDetails.setCallerName(callerName);
                callDetails.setCallerNumber(phNumber);
                callDetails.setCallDuration(callDuration);
                callDetails.setCallType(callType);
                callDetails.setCallTimeStamp(callDayTime);

          }
        }
            managedCursor.close();

        } catch (SecurityException e) {
            Log.e("Security Exception", "User denied call log permission");

        }

        return callDetails;

    }

这篇关于从通话记录中获取最近通话的详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆