我使用该代码的时间很长,只有很短的时间我从内容解析器获取null。我每次都在同一设备上运行此设备,但不是每次都遇到这种情况。在什么情况下,内容解析器会返回空游标。

static HashMap<String, String> getCallDetails(final String phoneNo,final Context context) {
        HashMap<String, String> dataMap = new HashMap<>();
        Uri callUri = Uri.parse("content://call_log/calls");
        final Cursor cursor = context.getContentResolver().query(callUri, null, null, null, CallLog.Calls.DATE + " DESC");
        int duration;
        String dateTime;
        String dTime;
        int typeFlag;
        String callType = "";
        try {
            if (cursor != null) {
                String newNumber;
                while (cursor.moveToNext()) {
                    // newNumber is the most recent dialed or call received number
                    newNumber = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
                    if (newNumber.contains(phoneNo)) {
                        duration = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.DURATION));
                        dTime = cursor.getString(cursor.getColumnIndex(CallLog.Calls.DATE));
                        typeFlag = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE));
                        switch (typeFlag) {
                            case CallLog.Calls.OUTGOING_TYPE:
                                callType = "OUTGOING";
                                break;
                        case CallLog.Calls.INCOMING_TYPE:
                            callType = "INCOMING";
                            break;
                    }
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
                    dateTime = formatter.format(new Date(Long.parseLong(dTime)));
                    dataMap.put("duration", String.valueOf(duration));
                    dataMap.put("dateTime", dateTime);
                    dataMap.put("callType", callType);
                    break;
                }
            }
        } else {
            appendLog("getCallDetails : cursor : cursor value is null");
        }
    } catch (Exception e) {
        String methodName = Thread.currentThread().getStackTrace()[2].getMethodName();
        String className = Thread.currentThread().getStackTrace()[2].getClassName();
        appendLog("Exception in " + className + " : " + methodName + " : " + e);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return dataMap;
}

最佳答案

尝试这个

Cursor cursor = context.getContentResolver().query(
            CallLog.Calls.CONTENT_URI, null, null, null,
            CallLog.Calls.DATE + " DESC");
    int number = cursor.getColumnIndex(CallLog.Calls.NUMBER);
    int type = cursor.getColumnIndex(CallLog.Calls.TYPE);
    int date = cursor.getColumnIndex(CallLog.Calls.DATE);
    int duration = cursor.getColumnIndex(CallLog.Calls.DURATION);


可能对您有用

关于android - 内容解析器在什么情况下会返回空游标?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47389470/

10-12 05:16