其他分享
首页 > 其他分享> > android-从通话记录中获取最近通话的详细信息

android-从通话记录中获取最近通话的详细信息

作者:互联网

我正在使用以下代码从通话记录中获取最近通话的详细信息.

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;

}

问题在于它返回的是最后一个第二个呼叫,而不是最后一个呼叫.我需要返回上一个电话.我用谷歌搜索,但无法获得完美的解决方案.请帮忙.提前致谢.

解决方法:

添加此行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;

    }

标签:calllog,android
来源: https://codeday.me/bug/20191026/1934790.html