Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Last Call Duration in android

I am looking for an easiest way to get call duration of last dialed number. So for e.g if I have made a call to my mom once I cut the call a notification with the duration should come up.

I am trying out the following but the problem is that it comes with a full list of duration. incoming, outgoing, missed.

How do I differentiate it:

I tried the following:

 private void getCallDetails() {

    StringBuffer sb = new StringBuffer();
    Cursor cur = getContentResolver().query( CallLog.Calls.CONTENT_URI,null, null,null, android.provider.CallLog.Calls.DATE + " DESC");

    int number = cur.getColumnIndex( CallLog.Calls.NUMBER ); 
    int duration = cur.getColumnIndex( CallLog.Calls.DURATION);
    sb.append( "Call Details : \n");
    while ( cur.moveToNext() ) {
        String phNumber = cur.getString( number );
        String callDuration = cur.getString( duration );
        String dir = null;

        sb.append( "\nPhone Number:--- "+phNumber +" \nCall duration in sec :--- "+callDuration );
        sb.append("\n----------------------------------");
    }
    cur.close();
    call.setText(sb);
}
like image 245
TheDevMan Avatar asked Oct 21 '13 11:10

TheDevMan


2 Answers

Try below code:

private void getCallDetails() {
        StringBuffer sb = new StringBuffer();
        Uri contacts = CallLog.Calls.CONTENT_URI;
        Cursor managedCursor = context.getContentResolver().query(contacts, 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()) {

            HashMap rowDataCall = new HashMap<String, String>();

            String phNumber = managedCursor.getString(number);
            String callType = managedCursor.getString(type);
            String callDate = managedCursor.getString(date);
            String callDayTime = new Date(Long.valueOf(callDate)).toString();
            // long timestamp = convertDateToTimestamp(callDayTime);
            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();
        System.out.println(sb);

You will get Call Type in below line:

int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
like image 172
Pratik Dasa Avatar answered Sep 17 '22 11:09

Pratik Dasa


This is my code and works perfectly fine for last outgoing call.

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

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

    if(dir.equals("OUTGOING")){
       //whatever you want here
        return "yes";
    }

    managedCursor.close();
    return "no";

}
like image 41
saketd403 Avatar answered Sep 20 '22 11:09

saketd403