How to use/access Phone Call Log in Android Application?
An Android Application can access the call log of the android
phone. This is possible because the call log details are
exposed by the Android platform as a content provider. Content
Providers in Android allow applications to share data across
applications (Refer:
https://android-app-market.com/content-provider-in-android.html). Numerous inbuilt content providers are there with the
Android platform. One such content Provider is the
CallLog.Calls content provider.
(Refer:
https://android-app-market.com/CallLog.Calls.html)
This article uses the CallLog.Calls content provider to access
the call details stored in an Android phone.
Accessing the Call Log in an Android Activity
The following code snippet is an Activity created in the Android Application that lists the call log.
public class CallsLister extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] projection = new String[] {CallLog.Calls.NUMBER, CallLog.Calls.DATE, CallLog.Calls.CACHED_NAME};
Uri contacts = CallLog.Calls.CONTENT_URI;
Cursor managedCursor = managedQuery(contacts, projection, null, null, CallLog.Calls.DATE + " ASC");
getColumnData(managedCursor);
...
//Code for other functionality continues...
...
...
}
private void getColumnData(Cursor cur){
try{
if (cur.moveToFirst()) {
String name;
String number;
long date;
int nameColumn = cur.getColumnIndex(CallLog.Calls.CACHED_NAME);
int numberColumn = cur.getColumnIndex(CallLog.Calls.NUMBER);
int dateColumn = cur.getColumnIndex(CallLog.Calls.DATE);
System.out.println("Reading Call Details: ");
do {
name = cur.getString(nameColumn);
number = cur.getString(numberColumn);
date = cur.getLong(dateColumn);
System.out.println(number + ":"+ new Date(date) +":"+name);
...
//Code for processing logic goes here
...
} while (cur.moveToNext());
}
}
finally{
cur.close();
}
}
}
Querying a content provider requires the following data:
- The URI for the provider which in our example is given by the constant CallLog.Calls.CONTENT_URI
- The names of the data fields we require. In our example above the column names selected are given by the constants CallLog.Calls.CACHED_NAME, CallLog.Calls.NUMBER and CallLog.Calls.DATE.
- The data types for these fields are available as String (details are available at the link https://android-app-market.com/CallLog.Calls.html which documents the type of each column of CallLog.Calls content provider.)
The above activity can access the call log only if the permission to read the contacts is enabled for this application. You need to modify the android manifest file to enable this permission. The listing below shows the manifest file of the above application.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.demo"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".EmergencyCallsLister"
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>
<uses-sdk android:minSdkVersion="2" />
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
</manifest>
Hey dude nice tutorial but it shows nothing
i m not much familiar with android.
thnxs
hi, great tutorial, I'm fairly new to android and I'm looking for some of the processing code and functionality code to manipulate as I haven't been coding for very long and am not great at writing complex clean code but rather use other code to understand. would you have the full code to look at?