Android Tutorial

(avery) #1

By : Ketan Bhimani


332 

calls.moveToFirst();
while (!calls.isAfterLast()) {
Log.d(DEBUG_TAG, “Duration: “ + calls.getInt(durIdx));
totalDuration += calls.getInt(durIdx);
calls.moveToNext();
}
Log.d(DEBUG_TAG, “HourlyClient123 Total Call Duration: “ +
totalDuration);


This code is similar to the code shown for the MediaStore audio
files.Again, we start with listing our requested columns: the call
label and the duration of the call. This time, however, we don’t
want to get every call in the log, only those with a label of
HourlyClient123.To filter the results of the query to this specific
label, it is necessary to specify the third and fourth parameters of
the managedQuery() call. Together, these two parameters are
equivalent to a database WHERE clause. The third parameter
specifies the format of the WHERE clause with the column name
with selection parameters (shown as ?s) for each selection
argument value. The fourth parameter, the String array, provides
the values to substitute for each of the selection arguments (?s) in
order as you would do for a simple SQLite database query.

As before, the Activity manages the Cursor object lifecycle. We use
the same method to iterate the records of the Cursor and add up all
the call durations.

Accessing Content Providers That Require Permissions

Your application needs a special permission to access the
information provided by the CallLog content provider. You can
declare the uses-permission tag using the Eclipse Wizard or by
adding the following to your AndroidManifest.xml file:

<uses-permission
xmlns:android=http://schemas.android.com/apk/res/android

Free download pdf