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.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity" >

   <Button
     android:id="@+id/bt_delete"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Delete Browser History"
     android:layout_centerInParent="true"
     android:onClick="deleteHistory"
      />

</RelativeLayout>

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();
//Where
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" />

How-to-delete-browsing-history-on-an-Android

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

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.