Zidane
Zidane

Reputation: 1904

how to determine whether last call was outgoing or incoming in android

I am using android call logs in my app and I would like to determine whether the last call was an incoming or out going call. This is what I have tried so far however int type gives me an error android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 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);
            }

Upvotes: 3

Views: 604

Answers (2)

Ashish Rawat
Ashish Rawat

Reputation: 5829

Try this

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

For details How do I access call log for android?

Upvotes: 1

prakash ubhadiya
prakash ubhadiya

Reputation: 1271

you are get

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

before chek if( managedCursor.moveToFirst()){} that's why you get exeption.

put this line inside of if or try this method

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

Upvotes: 0

Related Questions