Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在android中确定最后一个呼叫是传出还是传入_Java_Android_Android Studio_Logging_Mobile - Fatal编程技术网

Java 如何在android中确定最后一个呼叫是传出还是传入

Java 如何在android中确定最后一个呼叫是传出还是传入,java,android,android-studio,logging,mobile,Java,Android,Android Studio,Logging,Mobile,我正在我的应用程序中使用android通话日志,我想确定最后一次通话是来电还是外线通话。这是我到目前为止尝试过的,但是int type给了我一个错误android.database.CursorIndexOutOfBoundsException:Index-1请求,大小为284 Cursor managedCursor = context.getContentResolver().query( CallLog.Calls.CONTENT_URI,null, null,null, android.

我正在我的应用程序中使用android通话日志,我想确定最后一次通话是来电还是外线通话。这是我到目前为止尝试过的,但是int type给了我一个错误android.database.CursorIndexOutOfBoundsException:Index-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);
            }
你得到了

int type = Integer.parseInt(managedCursor.getString(managedCursor.getColumnIndex(CallLog.Calls.TYPE)));
检查前
if(managedCursor.moveToFirst()){}
这就是为什么你得到豁免

如果或尝试此方法,请将此行放在
的内部

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();
}
试试这个

public String getLastDialledNumber() {
    String[] projection = {Calls.NUMBER};
    Cursor cursor = mContext.getContentResolver().query(Calls.CONTENT_URI, projection,
            Calls.TYPE + "=" + Calls.OUTGOING_TYPE, null, Calls.DEFAULT_SORT_ORDER +
            " LIMIT 1");
    if (cursor == null) return null;

    if (cursor.getCount() < 1) {
        cursor.close();
        return null;
    }
    cursor.moveToNext();
    int column = cursor.getColumnIndexOrThrow(Calls.NUMBER);
    String number = cursor.getString(column);
    cursor.close();
    return number;
}
公共字符串getLastDialledNumber(){
字符串[]投影={Calls.NUMBER};
Cursor Cursor=mContext.getContentResolver().query(Calls.CONTENT\u URI,projection,
Calls.TYPE+“=”+Calls.OUTGOING_TYPE,null,Calls.DEFAULT_排序_顺序+
“限额1”);
if(cursor==null)返回null;
if(cursor.getCount()<1){
cursor.close();
返回null;
}
cursor.moveToNext();
int column=cursor.getColumnIndexOrThrow(Calls.NUMBER);
字符串编号=cursor.getString(列);
cursor.close();
返回号码;
}
详情

检查此项。谢谢你们似乎解决了我的问题。有人知道我为什么打5型电话吗?