在什么情况下,内容解析程序返回空游标



我使用这段代码已经很长时间了,只有少数时间我从内容解析器得到空。我每次都在同一台设备上运行它,但不是每次遇到这种情况时。在什么情况下,内容解析程序返回空游标。

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

可能对你有用

最新更新