Skip to content

Instantly share code, notes, and snippets.

@kannansuresh
Last active June 4, 2024 10:10
Show Gist options
  • Save kannansuresh/57945c9ee3fc29e04f62dc991449d595 to your computer and use it in GitHub Desktop.
Save kannansuresh/57945c9ee3fc29e04f62dc991449d595 to your computer and use it in GitHub Desktop.
Get Android phone call history/log programmatically

Source: AndroidDev

To get call history programmatically first add read contact permission in Manifest file:

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

Create xml file. Add the below code in xml file:

<Linearlayout android:layout_height="fill_parent"
 android:layout_width="fill_parent"
android:orientation="vertical">
<Textview android:id="@+id/call"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</Textview>
</Linearlayout>

Now call the getCallDetails() method in java class:

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

Using this application user can access his call history. It shows call number, call type i.e. incoming/outgoing/missed call, call date-time and call duration. Here is the output where user can see all details:

Sample Output

@netwest6445
Copy link

How to get CallLogs number Name?

@sahilweb24
Copy link

Which application we use for checking call history

@Gitam143
Copy link

Code please

@ab406
Copy link

ab406 commented Jun 22, 2023

String callerName = managedCursor.getColumnIndex( CallLog.Calls.CACHED_NAME );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment