Browser Content Provider Example

Code to check no. of url visited by user.

/res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TextView android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="@string/hello" />
</LinearLayout>

Activity file

package com.androidwork.browsercontentprovider;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Browser;
import android.util.Log;

public class BorwserContentProviderActivity extends Activity {
	/** Called when the activity is first created. */
	private static final String DEBUG_TAG = "VIEW2";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		String[] requestedColumns = { Browser.BookmarkColumns.TITLE,
				Browser.BookmarkColumns.VISITS,
				Browser.BookmarkColumns.BOOKMARK

		};
		Cursor faves = managedQuery(Browser.BOOKMARKS_URI, requestedColumns,
				null, null, null);
		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();
		int count = 0;
		while (!faves.isAfterLast()) {
		//	Log.d(DEBUG_TAG, "Inside while --- : " + count);
			Log.d("SimpleBookmarks",
					faves.getString(titleIdx) + " visited "
							+ faves.getInt(visitsIdx) + " times : "
							+ (faves.getInt(bmIdx) != 0 ? "true" : "false"));
			faves.moveToNext();
			count++;
		}
	}
}

Output

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s