Vishal V
Vishal V

Reputation: 31

How do I get recently completed phone Call details in android?

I want to get recently completed phone call details of android phone and after getting those details I want to start my new activity of my personal app when phone call has been completed . Basically i want to get phone call details before this details placed into directory/database of android phone.How to get this?

Upvotes: 3

Views: 1992

Answers (1)

Rushabh Patel
Rushabh Patel

Reputation: 3080

First you need to give the permission to read call logs from the device.

<uses-permission android:name="android.permission.READ_CONTACTS" />

Now use this method to get the recent call logs getCallDetails()

private void getCallDetails() {

    StringBuffer sb = new StringBuffer();
    Cursor managedCursor = managedQuery( CallLog.Calls.CONTENT_URI,null, null,null, null);
    int number = managedCursor.getColumnIndex( CallLog.Calls.NUMBER ); 
    int type = managedCursor.getColumnIndex( CallLog.Calls.TYPE );
    int date = managedCursor.getColumnIndex( CallLog.Calls.DATE);
    int duration = managedCursor.getColumnIndex( CallLog.Calls.DURATION);
    sb.append( "Call Details :");

    while ( managedCursor.moveToNext() ) {
        String phNumber = managedCursor.getString( number );
        String callType = managedCursor.getString( type );
        String callDate = managedCursor.getString( date );
        Date callDayTime = new Date(Long.valueOf(callDate));
        String callDuration = managedCursor.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;
        }

        sb.append( "\nPhone Number:--- "+phNumber +" \nCall Type:--- "+dir+" \nCall Date:--- "+callDayTime+" \nCall duration in sec :--- "+callDuration );
        sb.append("\n----------------------------------");
    }
    managedCursor.close();
    call.setText(sb);
}

Hope it will hemp you out for your need.

Upvotes: 4

Related Questions