Android Code to Read Call History Example


Introduction :
              Android can access call logs with just few lines of code. In other words call logs are built in content providers which can manage access to structured set of data. This tutorial is available with complete source code at the bottom to download.

Tutorial :
            The following tutorial reads android call logs using Cursors, by which we display Contact name, CallType, CallDate and Call Duration. It follows few simple steps as described below :

Step 1: Obtain required info from Android Call Log Content Provider
            To understand this step we need to have some background about Cursor in Android. Cursor represents results of a query pointing to one row of a result set. This way android can buffer the query results. Cursor provides various get*() methods that can be used to query the result sets.


Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null,null, null, null);

This returns a result-set with rows and column manner, using getColumnIndex() method, we can understand the column  index of various fields like phone number, call Type, call date and call duration.


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

Step 2 : Loop through the Cursor
               Loop through result through the index numbers obtained above to get the fields related to call logs.


while (managedCursor.moveToNext()) {
   String phNum = 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);
  }

Call code can be of 3 types Outgoing, Incoming and missed call. This can be interpreted as below:


switch (callcode) {
  case CallLog.Calls.OUTGOING_TYPE:
   callType = "Outgoing";
   break;
  case CallLog.Calls.INCOMING_TYPE:
   callType = "Incoming";
   break;
  case CallLog.Calls.MISSED_TYPE:
   callType = "Missed";
   break;
  }

Finally close the Cursor...

Step 4 : Display the required fields
            This section is open to your imaginations, I have displayed this in simple means using a string builder in a textView


sb.append("\nPhone Number:--- " + phNum + " \nCall Type:--- "
+ callType + " \nCall Date:--- " + callDate
+ " \nCall duration in sec :--- " + callDuration);
sb.append("\n----------------------------------");
textView.setText(sb);

Step 5 : Permissions in Androidmanifest.xml 
               Last but not the least, update Androidmanifest.xml for required permissions to read call logs.


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


Thus we have read android call log. Screenshot below for your reference.

Output Screen
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <TextView
            android:id="@+id/textview_call"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true" />
    </ScrollView>

</RelativeLayout>

MainActivity.java


package com.example.androidreadcallhistory;

import java.sql.Date;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.widget.TextView;

public class MainActivity extends Activity {
 TextView textView = null;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  textView = (TextView) findViewById(R.id.textview_call);
  getCallDetails();
 }

 private void getCallDetails() {
  StringBuffer sb = new StringBuffer();
  String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
  /* Query the CallLog Content Provider */
  Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null,
    null, null, strOrder);
  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 Log :");
  while (managedCursor.moveToNext()) {
   String phNum = managedCursor.getString(number);
   String callTypeCode = managedCursor.getString(type);
   String strcallDate = managedCursor.getString(date);
   Date callDate = new Date(Long.valueOf(strcallDate));
   String callDuration = managedCursor.getString(duration);
   String callType = null;
   int callcode = Integer.parseInt(callTypeCode);
   switch (callcode) {
   case CallLog.Calls.OUTGOING_TYPE:
    callType = "Outgoing";
    break;
   case CallLog.Calls.INCOMING_TYPE:
    callType = "Incoming";
    break;
   case CallLog.Calls.MISSED_TYPE:
    callType = "Missed";
    break;
   }
   sb.append("\nPhone Number:--- " + phNum + " \nCall Type:--- "
     + callType + " \nCall Date:--- " + callDate
     + " \nCall duration in sec :--- " + callDuration);
   sb.append("\n----------------------------------");
  }
  managedCursor.close();
  textView.setText(sb);
 }

}


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidreadcallhistory"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.androidreadcallhistory.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>


</manifest>

Source Code of this application
AndroidReadCallHistory.zip





Reactions:

8 comments :

  1. how to sync this call logs with mysql plz help

    ReplyDelete
  2. Great tutorial. How can i get the total duration for all calls made in the past 24 hours?

    ReplyDelete
  3. Can you please tell how can I display images also in call log

    ReplyDelete
  4. thank you so much...code works fine...just 1 issue...the call log entries dont contain any info regarding SMS messages...when i check the phone log it displays SMS logs also there....how to read the SMS log as well ?

    ReplyDelete