如何确定最后一次调用是否传出或传入的android

发布时间: 2016/9/27 23:06:01
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我使用的 android 调用登录我的应用程序并想确定最后一次调用是否传入或出去去调用。这是我一直到目前为止不过 int type 给我错误 android.database.CursorIndexOutOfBoundsException︰ 索引为-1 的要求,一个大小为 284

Cursor managedCursor = context.getContentResolver().query( CallLog.Calls.CONTENT_URI,null, null,null, android.provider.CallLog.Calls.DATE + " DESC");
            int number = managedCursor.getColumnIndex( CallLog.Calls.NUMBER );
            int duration1 = managedCursor.getColumnIndex( CallLog.Calls.DURATION);
            int type = Integer.parseInt(managedCursor.getString(managedCursor.getColumnIndex(CallLog.Calls.TYPE)));
            Log.v("DialBroadcast Receiver", "Number is: " + type);
            if( managedCursor.moveToFirst() == true ) {
                String phNumber = managedCursor.getString( number );
                callDuration = managedCursor.getString( duration1 );
                String dir = null;
                sb.append( "\nPhone Number:--- "+phNumber +" \nCall duration in sec :--- "+callDuration );
                sb.append("\n----------------------------------");
                Log.i("*****Call Summary******","Call Duration is:-------"+sb);
                Log.v("DialBroadcast Receiver", "Number is: " + callDuration);
            }

解决方法 1:

你是得到

int type = Integer.parseInt(managedCursor.getString(managedCursor.getColumnIndex(CallLog.Calls.TYPE)));

前赤 if( managedCursor.moveToFirst()){} 这就是为什么你获得核发。

把这条线内的 if 或尝试这种方法

private static String getCallDetails(Context context) {
    StringBuffer stringBuffer = new StringBuffer();
    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);       
    while (cursor.moveToNext()) {
        String phNumber = cursor.getString(number);
        String callType = cursor.getString(type);
        String callDate = cursor.getString(date);
        Date callDayTime = new Date(Long.valueOf(callDate));
        String callDuration = cursor.getString(duration);
        String dir = null;
        int dircode = Integer.parseInt(callType);
        switch (dircode) {
        case CallLog.Calls.OUTGOING_TYPE:
            dir = "OUTGOING";
            break;
        case CallLog.Calls.INCOMING_TYPE:
            dir = "INCOMING";
            break;

        case CallLog.Calls.MISSED_TYPE:
            dir = "MISSED";
            break;
        }
        stringBuffer.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- "
                + dir + " \nCall Date:--- " + callDayTime
                + " \nCall duration in sec :--- " + callDuration);
        stringBuffer.append("\n----------------------------------");
    }
    cursor.close();
    return stringBuffer.toString();
}
官方微信
官方QQ群
31647020