Start with
a basic project:
F:\work> mvn archetype:generate -DarchetypeCatalog=http://kallisti.eoti.org:8081/content/repositories/snapshots/archetype-catalog.xml
2: http://kallisti.eoti.org:8081/content/repositories/snapshots/archetype-catalog.xml -> galatea-archetype (null)
groupId: org.eoti.android.calls
artifactId: CallLogs
version: 1.0-SNAPSHOT
package: org.eoti.android.calls
F:\work> cd CallLogs
F:\work\CallLogs> mvn clean install
Add the
READ_CONTACTS permission to your
AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_CONTACTS" />
Add this to the end of your
onCreate() in
CallLogsActivity:
Uri allCalls = Uri.parse("content://call_log/calls");
Cursor c = managedQuery(allCalls, null, null, null, null);
for(String colName : c.getColumnNames())
Log.v(TAG, "Column Name: " + colName);
if (c.moveToFirst())
{
do{
String id = c.getString(c.getColumnIndex(CallLog.Calls._ID));
String num = c.getString(c.getColumnIndex(CallLog.Calls.NUMBER));
int type = Integer.parseInt(c.getString(c.getColumnIndex(CallLog.Calls.TYPE)));
switch (type)
{
case 1: Log.v(TAG, id + ", " +num + ": INCOMING") ; break;
case 2: Log.v(TAG, id + ", " +num + ": OUTGOING") ;break;
case 3: Log.v(TAG, id + ", " +num + ": MISSED") ; break;
}
} while (c.moveToNext());
}
Redeploy (
mvn clean install). If you watch the logs (
adb logcat) while running the app on the emulator, you will see the column names listed -- but not any call logs... How do we fix that?
V/CallLogs( 870): Column Name: numbertype
V/CallLogs( 870): Column Name: new
V/CallLogs( 870): Column Name: duration
V/CallLogs( 870): Column Name: _id
V/CallLogs( 870): Column Name: numberlabel
V/CallLogs( 870): Column Name: name
V/CallLogs( 870): Column Name: number
V/CallLogs( 870): Column Name: type
V/CallLogs( 870): Column Name: date
The easy answer is that you can dial a number on the emulator (even though it won't connect) and then re-run your CallLogs app to see the number you just dialed listed as an OUTGOING call.
What about incoming and missed? Create another AVD and launch a secondary emulator. In the titlebar and taskbar icon name, you can see some number associated with each emulator instance (like: 5554). From one emulator, you can "dial" the number of the other. So, for instance, in emulator 5556 open the dialer and dial 5554. Dial from one to the other; answer some not others; Dial the other direction, etc.
Run your app again. In my case, the emulator seemed to not log MISSED calls.
At some point, you are bound to wonder what those nulls are in the managed query.... from the API:
public final Cursor managedQuery(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
A few brief examples of what you can do (per this
tutorial):
uri: Who to get data from?
Uri allCalls = Uri.parse("content://call_log/calls");
projection: What columns to return?
String[] projection = new String[]{Calls._ID, Calls.NUMBER, Calls.TYPE};
selection: SQL WHERE clause
String selection = "Calls.NUMBER LIKE '65%'"; //---retrieve numbers beginning with 65---
String[] selectionArgs = null;
selectionArgs: (to populate the selection clause)
sortOrder: SQL ORDER BY clause
String sortOrder = "Calls.TYPE DESC"; //---sort result by call TYPE descending---
So, for example,
Cursor c = managedQuery(
Uri.parse("content://call_log/calls"),
new String[]{Calls._ID, Calls.NUMBER, Calls.TYPE},
"Calls.NUMBER LIKE '65%'",
null,
"Calls.TYPE DESC"
);
would return the
_id,
number,
type columns from numbers that started with
65 in
descending order....