Friday 12 June 2015

How to get the browser history.


                                     Browser history viewing using android 



This code working on google chrome only.


java file.   both of the code are working fine.


import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Browser;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class BrowserHistory extends Activity {

    Button btn_history;
   
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_browser_history);
       
       
       
        btn_history = (Button)findViewById(R.id.btn_history);
        btn_history.setOnClickListener(new View.OnClickListener() {
           
            @Override
            public void onClick(View v) {
               
               
                String[] proj = new String[] { Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL };
                String sel = Browser.BookmarkColumns.BOOKMARK + " = 0"; // 0 = history, 1 = bookmark
                Cursor mCur = getContentResolver().query(Browser.BOOKMARKS_URI, proj, sel, null, null);
                mCur.moveToFirst();
                String title = "";
                String url = "";
                if (mCur.getCount()> 0 && mCur !=null) {
                    boolean cont = true;
                    while (mCur.isAfterLast() == false && cont) {
                        title = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.TITLE));
                        url = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.URL));
                        // Do something with title and url
                       
                       
                        Log.i("History of Browser URL Titles:","Titles"+title);
                        Log.i("History of Browser URL Titles:","URL"+url);
                       
                       
                       
                        mCur.moveToNext();
                    }
                }




               
            }
        });
       

//this one for toast
        Cursor mCur =this.managedQuery(Browser.BOOKMARKS_URI,
                Browser.HISTORY_PROJECTION, null, null, null);
        mCur.moveToFirst();
        if (mCur.moveToFirst() && mCur.getCount() > 0) {
            while (mCur.isAfterLast() == false) {
                Toast.makeText(
                        BrowserHistory.this,
                        mCur.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX),
                        Toast.LENGTH_LONG).show();
                Toast.makeText(
                        BrowserHistory.this,
                        mCur.getString(Browser.HISTORY_PROJECTION_URL_INDEX),
                        Toast.LENGTH_LONG).show();
                mCur.moveToNext();
            }

        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.browser_history, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}



Androidmanifest.xml

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




If this code is not working.that is You getting error example. mCur null pointer exception in Runtime(RuntimeException). that time once your google chrome it open check the history data,then run again. It's work fine.








No comments:

Post a Comment