﻿ How to delete browsing history on an Android

How to delete browsing history on an Android

As you use a browser to open web pages every day, these web pages' information (e.g. title, url, and icon,...) are saved on your Android phones so that the internal storage of the phones have less available space.  In Android tip, I am going to show you how to delete browsing history of Android devices programmatically.

An Android phone stores browsing history and bookmark in a table called Browser.BOOKMARKS_URI that contains the following fields:
-BOOKMARK: a flat indicating that an item is a bookmark. The value of 1 indicates a bookmark and the value of 0 indicates a history.
-CREATED: the date the item was created.
-DATE: the date the item was last visited.
-FAVIION: the favicon of the item.
-TITLE: the visible title of the item.
-URL: the url of the item.
-VISITS: the number of time the item has been visited.
-_COUNT: the count of rows in a direcotory.
-_ID: the unique id of the item.

To delete browser history, you will use the delete(Uri url, String where, String whereArgs) method of the ContentResolver class. The url specifies the table that contains the history items to be deleted. The where is the WHERE clause to apply when deleting the rows. You can use ? symbols in where clause and replace them later in the whereArgs argument.

Now to have a workable example application that can be used to delete browsing history, create a new Android project. Then modify the activity_main.xml file to add a Button as shown below.

   tools:context=".MainActivity" >

     android:text="Delete Browser History"


In the MainActivity class, you will add a new method. The name of the method is deleteHistory. It will be called when you click the Button to delete the browsing history.

public void deleteHistory(View view){
//get ContentResolver
ContentResolver cr=getContentResolver();
String sel = Browser.BookmarkColumns.BOOKMARK + " =?";
//Selection args
String[] selArgs={"0"};// 0 = history, 1 = bookmark
//delete browsing history
int num_deleted=cr.delete(Browser.BOOKMARKS_URI, sel,selArgs);
Toast.makeText(this, num_deleted+" items are deleted.", Toast.LENGTH_SHORT).show();

Before running the example application, you must add the permission to read and write browser history and bookmark to the AndroidManifest.xml file.

<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />
   <uses-permission android:name="com.android.browser.permission.WRITE_HISTORY_BOOKMARKS" />


Posted by: Dara | post date: 08-03-2014 | Subject: Android Apps Development

write programming tip
