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.

25 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
    2. please provide code for all browser history.
      i can get only default browser history

      Delete
    3. This code working for Chrome and Internet explorer only,not for Mozilla Firefor.

      How to get the history from firefox.(kindly send yr reply to this mail also-jeeva2k10@gmail.com)

      Thanks

      Delete
    4. @Mandani Navneet, please share the source code to get only default browser history.

      Delete
    5. @jeeva nandham, not working for chrome or other, always cursor.getCount = 0;

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

    ReplyDelete
  8. in my project i need to get get date and time with browser history . when i try to get date and time it gives null .
    can you help me plz

    ReplyDelete
  9. Can you guide me on how to get the date and time for the browser history.
    This is my email: tansc6@gmail.com
    Thank you in advance.

    ReplyDelete
  10. can you tell me how to clear history and give me full code of java file

    ReplyDelete
  11. I'm getting both bookmarks and history as a single thing how can i differentiate both?

    ReplyDelete
  12. how to delete the browser history api level above 23

    ReplyDelete
  13. how to make a browser that stores it's history and bookmarks like other browser's uc browser and opera and dolphin

    ReplyDelete
  14. Those APIs were removed in the Android 7.0 SDK, IIRC. They were never a particularly good idea in the first place. Is there anything we can do?

    ReplyDelete