2011年5月16日月曜日

AndroidStudyMemo=Using the Browser Content Provider

Another useful, built-in content provider is the Browser.The Browser content provider

exposes the user's browser site history and their bookmarked websites.You access this content

provider via the android.provider.Browser class.As with the CallLog class, you

can use the information provided by the Browser content provider to generate statistics

and to provide cross-application functionality.You might use the Browser content

provider to add a bookmark for your application support website.

In this example,we query the Browser content provider to find the top five most frequently

visited bookmarked sites.

 

String[] requestedColumns = {

Browser.BookmarkColumns.TITLE,

Browser.BookmarkColumns.VISITS,

Browser.BookmarkColumns.BOOKMARK

};

 

Cursor faves = managedQuery(Browser.BOOKMARKS_URI, requestedColumns,

Browser.BookmarkColumns.BOOKMARK + "=1", null,

Browser.BookmarkColumns.VISITS + " DESC limit 5");

Log.d(DEBUG_TAG, "Bookmarks count: " + faves.getCount());

int titleIdx = faves.getColumnIndex(Browser.BookmarkColumns.TITLE);

int visitsIdx = faves.getColumnIndex(Browser.BookmarkColumns.VISITS);

int bmIdx = faves.getColumnIndex(Browser.BookmarkColumns.BOOKMARK);

faves.moveToFirst();

while (!faves.isAfterLast()) {

Log.d("SimpleBookmarks", faves.getString(titleIdx) + " visited "

+ faves.getInt(visitsIdx) + " times : "

+ (faves.getInt(bmIdx) != 0 ? "true" : "false"));

faves.moveToNext();

}

 

Again, the requested columns are defined, the query is made, and the cursor iterates

through the results.

Note that the managedQuery() call has become substantially more complex. Let's take

a look at the parameters to this method in more detail.The first parameter,

Browser.BOOKMARKS_URI, is a URI for all browser history, not only the Bookmarked

items.The second parameter defines the requested columns for the query results.The

third parameter specifies that the bookmark property must be true.This parameter is

needed in order to filter within the query. Now the results are only browser history entries

that have been bookmarked.The fourth parameter, selection arguments, is used only

when replacement values are used, which is not used in this case, so the value is set to

null. Lastly, the fifth parameter specifies an order to the results (most visited in descending

order). Retrieving browser history information requires setting the

READ_HISTORY_BOOKMARKS permission.

 

 

 

0 件のコメント:

コメントを投稿