Pages

Thursday, November 14, 2013

Browser History and bookmarks

In this post, you will learn to create a simple Android app to view browser history and bookmarks. This app displays the browser history and bookmarks in a single list. It can also be used to clear the browser history from your Android device. In addition, the user is able to click a link on the list to open the page on the browser.
To develop this app, you will need to create an Android project in Eclipse. The name of the project or application will be BClean. The BClean app has the simple user interface. When the app firstly launches, it displays a list of browser history and bookmarks. You will need to edit the activity_main.xml file to include a ListView component. Here is the content of the activity_main.xml file.

activity_main.xml file

<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" >

    <ListView
     
        android:id="@+id/hb_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
     
    </ListView>

</RelativeLayout>


Each item of the list will display three fields of each browser history or bookmark. The first field is the icon. The second field is the title and the third field is the url.

browser history and bookmarks view and clean


To customize the ListView component to allow each row or item to show the data in that way, you will need the listlayout.xml and ListAdapterModel.java files. The listyout.xml file represents the layout of each row or item of the list. There are three components in the listlayout file. The first component is an ImageView. It is for displaying the icon. The second and third components are TextView compoennts for displaying the title and url.

listlayout.xml file

<?xml version="1.0" encoding="utf-8"?>
<!--  Single List Item Design -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dip" >
<ImageView
    android:id="@+id/icon"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:padding="5sp"
    android:contentDescription="icon_image"
 />
<LinearLayout
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:orientation="vertical"
    >

<TextView
       android:id="@+id/hbtitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="1sp"
        android:textSize="20sp"
        android:scrollHorizontally="true"
    android:singleLine="true"
        android:textStyle="bold" >
</TextView>
<TextView
       android:id="@+id/hburl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="1sp"
        android:textSize="15sp"
        android:scrollHorizontally="true"
    android:singleLine="true"
        android:textColor="#0000ff">
 </TextView>
</LinearLayout>
</LinearLayout>

The ListAdapterModel.java file defines a class called ListAdapterModel that extends ArrayAdapter class. The object of the ListAdapterModel class will be used as the data source of the list. Below is the content of the ListAdapterModel. java file.

ListAdapterModel. java file

package com.example.bclean;

import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class ListAdapterModel extends ArrayAdapter<String>{
int groupid;
ArrayList<String> titles;
ArrayList<String> urls;
ArrayList<Bitmap> bitmaps;
Context context;
String path;
public ListAdapterModel(Context context, int vg, int id, ArrayList<String> titles,ArrayList<String> urls,ArrayList<Bitmap> bitmaps){
super(context,vg, id, titles);
this.context=context;
groupid=vg;
this.titles=titles;
this.urls=urls;
this.bitmaps=bitmaps;

}
public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView = inflater.inflate(groupid, parent, false);
        ImageView imageView = (ImageView) itemView.findViewById(R.id.icon);
        imageView.setImageBitmap(bitmaps.get(position));
        TextView textTitle= (TextView) itemView.findViewById(R.id.hbtitle);
        String title=titles.get(position);
        textTitle.setText(title);
        TextView textURL = (TextView) itemView.findViewById(R.id.hburl);
        String url=urls.get(position);
        textURL.setText(url);
        //make the url clickable
        Linkify.addLinks(textURL, Linkify.ALL);
        return itemView;
}


}

The most important file in this application is MainActivity.java file. This file contains code that reads, display  the browser history and bookmarks, and clean the history data.
In the onCreate method of the MainActivity class, three ArrayList objects are created by calling the createLists method. One ArrayList object is to store the titles of the browser history and bookmarks. One is for storing the urls. And the last one will store the icons. The three ArrayList objects will be supplied to the ListAdapterModel class to show the browser history and bookmarks on the list.

MainActivity.java file

package com.example.bclean;
import java.util.ArrayList;
import android.os.Bundle;
import android.provider.Browser;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {

    private ArrayList<String> titles;
    private ArrayList<String> urls;
    private ArrayList<Bitmap> bitmaps;
    private ContentResolver cr;
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        createLists();
       }
    protected void onResume(){
    super.onResume();
    getBH();      
    showHistoryBookmarks();
   
    }
 
    public void createLists(){
    titles=new ArrayList<String>();
    urls=new ArrayList<String>();
    bitmaps=new ArrayList<Bitmap>();
 
    }
    public void getBH(){
    Bitmap icon;
    cr=getContentResolver();
    String order=Browser.BookmarkColumns.DATE+" DESC";
    String[] projection={Browser.BookmarkColumns.TITLE,Browser.BookmarkColumns.URL,Browser.BookmarkColumns.FAVICON};
    //String selection=projection[0]+"=?";
    //String args[]={"Google"};
    Cursor rows=cr.query(Browser.BOOKMARKS_URI,projection, null,null,order);
    if(rows.getCount()>0){
    while(rows.moveToNext()) {
    //read title
    String title=rows.getString(rows.getColumnIndex(projection[0]));
    //read url
    String url=rows.getString(rows.getColumnIndex(projection[1]));
    //read icon
    byte[] bicon=rows.getBlob(rows.getColumnIndex(projection[2]));
    if(bicon!=null){
    //convert blob image data to Bitmap
    icon=BitmapFactory.decodeByteArray(bicon,0,bicon.length);  
   
    }
   
    else{
    //default icon for history and bookmarks that do not icons
    icon=BitmapFactory.decodeResource(getResources(),R.drawable.noicon);
    }
    //add to lists
    addToList(title,url,icon);
    }
    //close the cursor
    rows.close();
    }    
   
   
    }  
 
 
    public void addToList(String title,String url, Bitmap bitmap){
   
    titles.add(title);
    urls.add(url);
    bitmaps.add(bitmap);
   
    }

 
    public void showHistoryBookmarks(){
    ListView l=(ListView) findViewById(R.id.hb_list);
if(l!=null){
if(titles.size()>0){
ListAdapterModel aa=new ListAdapterModel(this,R.layout.listlayout,R.id.hbtitle,titles,urls,bitmaps);  
l.setAdapter(aa);
}
else{
Toast.makeText(this, "This is no bookmark or history.", Toast.LENGTH_SHORT).show();
}
}

}
 
    public void cleanHB(){
    if(Browser.canClearHistory(cr)){
    Browser.clearHistory(cr); //clear history data
    createLists(); //recreate the lists
    onResume(); //update the list
    }
   
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
 
    public boolean onOptionsItemSelected(MenuItem item){
    if(item.getItemId()==R.id.action_clean)
    {
    cleanHB();
   
    }
    return false;
    }

 
}

The getHB method reads the browser history and bookmarks from Android and add the data (titles, urls, and icons) on the list. To get the browser history and bookmarks from Android, you will use the ContentResolver class. The instance of this class can be obtained by invoking the getContentResolver method from the current activity. By using the query method of the ContentResolver class, you can read all browser history and bookmarks. These history and bookmarks are stored in a Cursor object. The query method has five arguments. The first argument accepts the url that the data will be read from. This app reads the browser history and bookmarks. Thus, the url is BOOKMARKS_URI from the Browser class. The second argument allows you to specifiy an array of fields or column names to be retrieved. In this app, their are only three fields that will be retrieved TITLE, URL, and FAVICON. You will need to use the BookmarkColumns class in Browser class to access the field names of the table that stores the browser history and bookmarks. The third and forth arguments specifies the selection (field and operator) and selection arguments (values) to retrieve the rows of the table. Since the app retrieves all rows, you will use null values for these arguments. The last argument of the query method allows you to specify the field that the rows will be ordered by.
The title, and urls are text (string). The icon field is of Blob type. The data in Blob type can be converted to the Bitmap by using the decodeArrayByte method of the BitmapFactory class.
The showHistoryBookmarks will be called after the getHB method to display the titles, urls, and icons of the browser history and bookmarks on the list. In this method, the ListAdapterModel object is created and the titles, urls, and icons are passed to it.
The cleanHB method is called when the user touches the clean menu item. It will remove all history data from Android. The list will be updated to reflect the change by calling the onResume method. You will need to edit the main.xml file (in menu folder) to add the clean item to the menu. Here is the content of the main.xml file.
main.xml file

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_clean"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_clean"/>


</menu>

Before running the BClean app, you also need to edit the AndroidManifest.xml file to allow the application to read and write browser history and bookmarks. Please add the following permissions to the file.

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


Download the apk file of the BClean app

Merge or Combine PDF, Txt, Images

48 comments:

  1. Great post, thanks!!
    Can you let me know how to add date and time stamps?
    also how to export the browser history list to a text file on the sd card?

    Adam

    ReplyDelete
    Replies
    1. Ok i have managed to get the date/time stamp in the list. But now instead of exporting this list to SD card, is there any way to send this list automatically via email at a scheduled time?

      Delete
    2. How to add date&time stamp in the list?

      Delete
  2. Great Post!!!!
    Thanks for your valuable posting, it was very informative. Am working in Erp Software Company In India

    ReplyDelete
  3. does it takes browser history and bookmarks from all the browsers like chrome,mozilla,ucbrowser etc

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Vương Lâm nói tên ra, thiếu niên nghe xong, nhất thời ngạc nhiên, thất thanh nói:

    - Ngươi chính là Vương Lâm hả, phế... kia dựa vào tự sát mới tiến vào Hằng Nhạc Phái.

    Nói đến đây, hắn cảm thấy ngượng, cười nhẹ nói:

    - Bạn hữu, ta gọi là Trương Hổ, đừng để trong lòng, hiện tại trong phái hầu như không ai là không biết ngươi, ngươi cũng đừng trách ta vừa rồi nói như vậy, thực ra ta rất bội phục ngươi, lại có thể sử dụng như vậy phương pháp tiến vào.
    học kế toán thực hành
    forum rao vặt cattleya
    học kế toán tổng hợp
    chung cư eco-green-city
    học kế toán thực hành
    học kế toán tại bắc ninh
    dịch vụ kế toán trọn gói
    chung cư hà nội
    dịch vụ làm báo cáo tài chính
    kế toán cho quản lý
    khoá học kế toán thuế
    keny idol
    trung tâm kế toán tại long biên
    trung tâm kế toán tại hải phòng

    Vương Lâm cười khổ, cũng không thanh minh, lại đưa qua mấy củ khoai lang.

    Trương Hổ vội vàng tiếp nhận, sau ăn mấy khẩu ngượng ngùng nói:

    - Vương Lâm, ngươi vẫn là chừa cho mình chút đi, ngươi mới tới đây, không biết con chồn tạp vụ ở đây trong lòng tâm địa gian xảo, chờ ngày mai ngươi làm việc sẽ biết rõ, con mẹ nó, hắn quả thực sẽ không nhìn những đệ tử ký danh chúng ta như con người!
    Chương 11: Trương Hổ
    - Con chồn?

    Vương Lâm giật mình, người thứ nhất hắn nghĩ đến chính là người thanh niên áo vàng lúc trước cười nhạo mình, cảm thấy hắn có vẻ phù hợp với danh hiệu này, nhưng trong lòng không biết rốt cuộc đối phương nói có đúng là hắn ta hay không.

    - A? Ngươi chưa nhìn thấy hắn? Chính là thằng cha phụ trách an bài công tác của ký danh đệ tử, hắn cũng là ký danh đệ tử, chẳng qua đã đạt được tư cách tu luyện tiên pháp, mặc áo vàng, vừa nhìn đã thấy không phải là người tốt gì, giống như một con chồn con, chúng ta thường ở đằng sau vụng trộm gọi hắn như vậy.

    Trương Hổ nuốt nước miếng, nghiến răng nghiến lợi giải thích.

    ReplyDelete
  6. When I enter this source code on Android Studio it does some errors

    ReplyDelete
  7. I always like and search such topics and everything connected to them.Excellent and very cool idea and the subject at the top of magnificence ..
    Weblogic Training in Chennai

    ReplyDelete
  8. why it shows only local history? how to show full history from google acount ?

    ReplyDelete
  9. Your post, regarding Android App Development is very effective for learners. I have got some important suggestions from it. I'm working in Brave Technologies Private Limited.BRAVE TECHNOLOGIES is a leading ERP software company offering best quality cost effective software applications and end to end enterprise solutions. We are experts in Mobile App development on both platforms – Android & iOS.

    ReplyDelete
  10. Hello, Thanks for this valuable post. It's working fine in Android 4.0.1 version. Not working with greater then android 4.1 version. Can you suggest me how to get browser history for Android 5.0 or greater than this.

    ReplyDelete
  11. Hello. Do you know how to get the history and bookmarks option in case of an Android Dictionary App... that has an option to access the history of the recently searched words and also an option to bookmark a particular word for quick access?

    ReplyDelete
  12. I am expecting more interesting topics from you. And this was nice content and definitely it will be useful for many people.
    Mobile App Development Company
    Mobile App Development Company in India
    Mobile App Development Companies

    ReplyDelete
  13. problems with BookmarkColumns...help pls

    ReplyDelete
    Replies
    1. Your Error is the same of what i got my project have you reach on solution and share with me

      Delete
  14. Thank you for your precious information. very clear explanation.Please share more like that..
    Construction Services

    ReplyDelete
  15. I have learnt quite a lot about creating a simple Android app to view browser history and bookmarks by reading this post. Many thanks for sharing this invaluable information. Feel free to click on Professional Writing Services | Writing Academic Papers

    ReplyDelete
  16. Hello Admin I have tried to build this history project through all the steps you share us but when i reach on in MainActivity GiHB method >> BookmarkColumns it show me an error may you help me to fix this

    ReplyDelete
  17. Amazing Article ! I have bookmarked this article page as i received good information from this. All the best for the upcoming articles. I will be waiting for your new articles. Thank You ! Kindly Visit Us @ Coimbatore Travels | Ooty Travels | Coimbatore Airport Taxi | Coimbatore taxi | Coimbatore Taxi

    ReplyDelete
  18. I love it... I've never tried the texture paste before but now I will. Love the numbers!

    ReplyDelete
  19. I love this post.


    โปรโมชั่นGclub ของทางทีมงานตอนนี้แจกฟรีโบนัส 50%
    เพียงแค่คุณสมัคร Gclub กับทางทีมงานของเราเพียงเท่านั้น
    ร่วมมาเป็นส่วนหนึ่งกับเว็บไซต์คาสิโนออนไลน์ของเราได้เลยค่ะ
    สมัครสล็อตออนไลน์ >>> goldenslot
    สนใจร่วมลงทุนกับเรา สมัครเอเย่น Gclub คลิ๊กได้เลย

    ReplyDelete
  20. Great post very informative thanks for sharing.

    https://www.skyhidev.com/website-development-vancouver/

    ReplyDelete
  21. I appreciate your effort and want you to keep on posting such posts
    Android internship in kochi

    ReplyDelete
  22. This comment has been removed by the author.

    ReplyDelete
  23. Thank you for sharing in this article Great extra tips i am working in a Website Development Company Vancouver

    ReplyDelete
  24. Thank you for the information you provide, it helped me a lot! it's great


    ios development course in kochi

    ReplyDelete
  25. Prior to stumbling upon this post, I didn't know that creating an Android app to view browser history and bookmarks can be that simple. Thanks a lot for sharing this information. Feel free to click on Professional Marketing Company. We assure you that once you click on that link, you will get online marketing services that will meet if not surpass your expectations.

    ReplyDelete
  26. Your blog posts are more interesting and impressive. I think there are many people like and visit it regularly, including me.I actually appreciate your own position and I will be sure to come back here.

    ecommerce development company in kochi

    ReplyDelete
  27. well written article. I’ll be sure to bookmark it and return to read more of your useful info. Thanks for the post. I will definitely return.

    website development company in kochi

    ReplyDelete
  28. Its very best of best content and, I learn some thing very complicated on different blogs everyday. I think You put a lot of effort to create this topic blog. I appreciate your work and, thank you so much for this sharing. best assignment help -
    plagiarism checker -
    assignment writing help

    ReplyDelete
  29. Striking to take a gander at your blog once more, it's been an especially drawn out time period for me. I need this article to complete my school task. Appreciative to you. The user is 24 * 7 available on affected devices. Check the simple solution if there is Hp Error Code oxc19a0003.

    ReplyDelete
  30. This is the kind of article information to read whenever you want to understand more about outsourcing. Personally, I have really benefited and so marvelous from reading it. I can’t wait to read more of your new content. Thanks for sharing and Good luck for new updates. taxation law assignment help -
    corporate finance assignment help -
    nursing assignment help

    ReplyDelete
  31. While utilizing the printer in the event that it stalls out in the center of the work, it can raise the temper of the user. We have seen numerous canon putting their questions dependent on canon printer in error state issue, for instance what do I do if my standard printer is in error state?, how would I get my ordinance printer out of error state, etc. Subsequently, to settle every one of these inquiries we have the arrangement and it is referenced beneath.
    canon printer in error state

    ReplyDelete
  32. This is very very helpful content information for all of everyone. Thank you very much for sharing this very superb topic share with us. You should take part in a contest for one of the most useful sites online. I’m going to highly recommend this website all articles! Thanks. assignment provider -
    healthcare management assignment help -
    childcare assignment help

    ReplyDelete
  33. Hi there very nice website!! Guy .. Excellent .. Amazing ..
    mobile app development company in saudi arabia

    ReplyDelete
  34. Great post!

    https://globosoft.in/gKart-complete-ecommerce-solution
    https://globosoft.in/

    ReplyDelete