2011年5月16日月曜日

AndroidStudyMemo=Using the CallLog Content Provider

Android provides a content provider to access the call log on the handset via the class

android.provider.CallLog. At first glance, the CallLog might not seem to be a useful

provider for developers, but it has some nifty features.You can use the CallLog to filter recently

dialed calls, received, and missed calls.The date and duration of each call is logged

and tied back to the Contact application for caller identification purposes.

The CallLog is a useful content provider for customer relationship management

(CRM) applications.The user can also tag specific phone numbers with custom labels

within the Contact application.

To demonstrate how the CallLog content provider works, let's look at a hypothetical

situation where we want to generate a report of all calls to a number with the custom

262 Chapter 11 Sharing Data Between Applications with Content Providers

labeled HourlyClient123.Android allows for custom labels on these numbers, which we

leverage for this example:

String[] requestedColumns = {

CallLog.Calls.CACHED_NUMBER_LABEL,

CallLog.Calls.DURATION

};

Cursor calls = managedQuery(

CallLog.Calls.CONTENT_URI, requestedColumns,

CallLog.Calls.CACHED_NUMBER_LABEL

+ " = ?", new String[] { "HourlyClient123" } , null);

Log.d(DEBUG_TAG, "Call count: " + calls.getCount());

int durIdx = calls.getColumnIndex(CallLog.Calls.DURATION);

int totalDuration = 0;

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

 

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"

</uses-permission>

Although it's a tad confusing, there is no CallLog permission. Instead, applications that access

the CallLog use the READ_CONTACTS permission.Although the values are cached

within this content provider, the data is similar to what you might find in the contacts

provider.

 

0 件のコメント:

コメントを投稿