Tuesday, 19 June 2012

How to get Browser History in Android?



Call below function.

public void getBrowserHist()  {
        Cursor mCur = managedQuery(Browser.BOOKMARKS_URI,
                Browser.HISTORY_PROJECTION, null, null, null);
        mCur.moveToFirst();
        if (mCur.moveToFirst() && mCur.getCount() > 0) {
            while (mCur.isAfterLast() == false) {
                Log.v("titleIdx", mCur
                        .getString(Browser.HISTORY_PROJECTION_TITLE_INDEX));
                Log.v("urlIdx", mCur
                        .getString(Browser.HISTORY_PROJECTION_URL_INDEX));
                mCur.moveToNext();
           }
      }
}


Add below uses-permission into your manifest file.

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

Enjoy :-)

Don’t forget to provide feedback or follow this blog, if you find this blog is useful.

13 comments:

  1. where to implement this code?
    thanks.

    ReplyDelete
    Replies
    1. Hello,

      You can Call above function from your button click event of GetHistory Button or call it from your onCreate() method also.

      Delete
  2. How i can use ListView for history and bookmarks?

    ReplyDelete
    Replies
    1. Hello,

      Store Titles and Urls into array and then you can easily display this data in listview.

      Delete
    2. I need fast some code for it!
      Please write some easy code.
      I was try many codes, but I still failed!

      Delete
    3. Hello,

      First Define 2 Arraylist, first for Titles and second for Urls

      ArrayList mTitles=new ArrayList();
      ArrayList mUrls=new ArrayList();

      and then write below code for add Titles and Urls into while loop.

      mTitles,add(mCur.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX));
      mUrls,add(mCur.getString(Browser.HISTORY_PROJECTION_URL_INDEX));

      Delete
  3. Can you provide me the full code with main and manifest file.??

    ReplyDelete
    Replies
    1. Activity_main::---







      --------------------------------------------------------
      MainActivity.java::------


      import java.util.ArrayList;

      import android.os.Bundle;
      import android.provider.Browser;
      import android.app.Activity;
      import android.database.Cursor;
      import android.util.Log;
      import android.view.Menu;
      import android.widget.TextView;

      public class MainActivity extends Activity {

      @Override
      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      TextView view = (TextView) findViewById(R.id.hello);
      String[] projection = new String[] {
      Browser.BookmarkColumns.TITLE
      , Browser.BookmarkColumns.URL
      };
      Cursor mCur = managedQuery(android.provider.Browser.BOOKMARKS_URI,
      projection, null, null, null
      );
      mCur.moveToFirst();
      int titleIdx = mCur.getColumnIndex(Browser.BookmarkColumns.TITLE);
      int urlIdx = mCur.getColumnIndex(Browser.BookmarkColumns.URL);
      while (mCur.isAfterLast() == false) {
      view.append("n" + mCur.getString(titleIdx));
      view.append("n" + mCur.getString(urlIdx));
      mCur.moveToNext();
      }

      }
      }
      --------------------------------------------------------
      Manifest.xml:::-----------




















      Delete
  4. please I would be surely thankful to you if you could please provide me with the source code
    thanks

    ReplyDelete
  5. Hi
    Is it possible to get other browsers history(not default browser) in android mobile?

    ReplyDelete
  6. in my code we can get all the browser history..

    ReplyDelete
    Replies
    1. please any body provide the source code for favorites or bookmark in android.

      Delete
  7. This comment has been removed by the author.

    ReplyDelete