Pregunta

I've written a simple web browser that accepts urls and renders web pages. How can I store the history of past page views. Can someone suggest a simple solution?

¿Fue útil?

Solución 3

Perhaps you could look to an open source project like the Zirco Browser -> https://code.google.com/p/zirco-browser/

Some code like this > https://code.google.com/p/zirco-browser/source/browse/branches/tint-browser-old2/src/org/tint/controllers/BookmarksHistoryController.java

   /**
         * Get a Cursor the history, e.g. records wich have a visits count > 0. Sorted by last visited date.
         * @param currentActivity The parent activity.
         * @return A Cursor to history records.
         * @see Cursor
         */
        public Cursor getHistory(Activity currentActivity) {
                String whereClause = Browser.BookmarkColumns.VISITS + " > 0";
                String orderClause = Browser.BookmarkColumns.DATE + " DESC";

                return currentActivity.managedQuery(android.provider.Browser.BOOKMARKS_URI, Browser.HISTORY_PROJECTION, whereClause, null, orderClause);
        }

Perhaps you could copy some code and use it for your project.

Otros consejos

You could use a doubly-linked LinkedList to maintain a history, but WebView already contains methods to deal with forward and back history.

void     goBack()

Goes back in the history of this WebView.

void     goBackOrForward(int steps)

Goes to the history item that is the number of steps away from the current item.

void     goForward()

Goes forward in the history of this WebView.

From the docs: https://developer.android.com/reference/android/webkit/WebView.html

// put this in your xml file...

<WebView
    android:id="@+id/web_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

// then in your code

WebView webView = (WebView) findViewById(R.id.web_view);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient()
{
    // Links clicked will be shown on the webview
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url)
    {
        return super.shouldOverrideUrlLoading(view, url);
    }
}

// Then to load the webpage from edit text would be
webView.loadUrl(--Edit Text URL here);

// To go back to your previous page, call this method
webView.goBack();

// To go to your next page would be
webView.goForward();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top