How to read call logs programmatically from android

It’s fairly easy. You need add following uses-permission in android manifest to get call history programmatically.

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

That’s all here.

And then create an activity and layout. We need to query from ContentProvider. And for that I used CursorClassLoader.

Read about CursorLoader from here:  http://developer.android.com/training/load-data-background/setup-loader.html

 

I want to load 4 properties and they are- phone Number, type of call eg. Outgoing or ingoing or missed call, Date and Time of the call and duration of the call.

So first use LoaderManager.LoaderCallbacks<Cursor> interface in your activity. It has three methods.

abstract Loader        onCreateLoader(int id, Bundle args)

//Instantiate and return a new Loader for the given ID.

abstract void      onLoadFinished(Loader loader, D data)

//Called when a previously created loader has finished its load.

abstract void      onLoaderReset(Loader loader)

//Called when a previously created loader is being reset, and thus making its data unavailable.

To initialize a query, we need to call LoaderManager.initLoader() at the very first place.

We are going to add a button and call this in that button events here and after this background framework will be initialized. As soon as the background framework is initialized, it calls your implementation of onCreateLoader(). To start the query, we have to return a CursorLoader from this method.

 


@Override
public Loader onCreateLoader(int loaderID, Bundle args) {
Log.d(TAG, "onCreateLoader() >> loaderID : " + loaderID);

switch (loaderID) {
case URL_LOADER:
// Returns a new CursorLoader
return new CursorLoader(
this,   // Parent activity context
CallLog.Calls.CONTENT_URI,        // Table to query
null,     // Projection to return
null,            // No selection clause
null,            // No selection arguments
null             // Default sort order
);
default:
return null;
}
}

 

We are going access our expected data from a Cursor. And we will get this in the onLoadFinished() method.

 


@Override
    public void onLoadFinished(Loader loader, Cursor managedCursor) {
        Log.d(TAG, "onLoadFinished()");

        StringBuilder sb = new StringBuilder();

        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("<h4>Call Log Details <h4>");
        sb.append("\n");
        sb.append("\n");

        sb.append("<table>");

        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 callTypeCode = Integer.parseInt(callType);
            switch (callTypeCode) {
                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("<tr>")
                    .append("<td>Phone Number: </td>")
                    .append("<td><strong>")
                    .append(phNumber)
                    .append("</strong></td>");
            sb.append("</tr>");
            sb.append("<br/>");
            sb.append("<tr>")
                    .append("<td>Call Type:</td>")
                    .append("<td><strong>")
                    .append(dir)
                    .append("</strong></td>");
            sb.append("</tr>");
            sb.append("<br/>");
            sb.append("<tr>")
                    .append("<td>Date & Time:</td>")
                    .append("<td><strong>")
                    .append(callDayTime)
                    .append("</strong></td>");
            sb.append("</tr>");
            sb.append("<br/>");
            sb.append("<tr>")
                    .append("<td>Call Duration (Seconds):</td>")
                    .append("<td><strong>")
                    .append(callDuration)
                    .append("</strong></td>");
            sb.append("</tr>");
            sb.append("<br/>");
            sb.append("<br/>");
        }
        sb.append("</table>");

        managedCursor.close();

        callLogsTextView.setText(Html.fromHtml(sb.toString()));
    }

 

Output:

 

device-2014-04-17-013232

 

Full Source code:  https://github.com/rokon12/call-log

 

Reference: http://android2011dev.blogspot.com/2011/08/get-android-phone-call-historylog.html

Advertisements

2 Comments

  1. Nice tutorial

    How do i find duration in this way HH:MM:SS

    I want to get it in hours:minutes:seconds

    I can see you using the LOG to get hold of it, but I don’t want it in that way.

    Help will be highly appreciated

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s