将 webview 内容保存到 android 存储并加载它 [英] Save webview content to android storage and load it

查看:54
本文介绍了将 webview 内容保存到 android 存储并加载它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个具有 webview 布局的 android 应用程序.这是我申请的标准:

I want to make an android application which has a webview layout. This is the criteria of my application:

  1. 应用程序第一次启动时,webview 会加载一个 url(可能是 facebook、google 等.)webview.loadUrl("http://www.google.com");

  1. The first time the application starts, webview loads an url (maybe facebook, google, etc..) webview.loadUrl("http://www.google.com");

加载url后,应用程序将加载的url保存到android内部存储中特定位置"的HTML视图(file.htm)中.所以,假设我打开google.com",应用程序将 google 的网页保存到 HTML 文件(假设文件名,google.htm"),当我去那个特定的地方"并点击google.htm"文件,它使用android的HTML查看器显示谷歌网页.

After it loads the url, the application saves the loaded url to HTML View (file.htm) in a "specific place" in android's internal storage. So, let's say i open "google.com", the application saves the google's web page to HTML file (let's say the filename, "google.htm"), and when i go to that "specific place" and click the "google.htm" file, it shows the google web page using android's HTML Viewer.

当应用程序再次启动时,或者简单地说应用程序再次加载 url(在本例中为google.com"),它不会从google.com"页面获取,而是从内部存储android上的google.htm"文件.因此,从用户的角度来看,该应用程序仍然可以加载网页,即使它没有连接到互联网.

When the application starts again, or simply say the application loads the url again (in this case, "google.com") , it doesn't take from the "google.com" page BUT it takes from the "google.htm" file on the internal storage android. So from the user's view, that application can still load webpages, even though it's not connected to internet.

为了简单起见,

  1. 应用程序启动 -> 转到指定的 url -> 检查存储
  2. 如果指定的 url 在存储中包含 HTML 文件,则从存储中加载
  3. 否则它会从网络加载 url.

谁能帮我提供代码和解释?对此,我真的非常感激.谢谢大家:D

Can anyone help me with the code and explanation? I really appreciate it. Thanks guys :D

推荐答案

您可以使用 WebView 的 Javascript 接口在页面加载完成后返回整个 HTML 源代码.为此,您需要将自己的 WebViewClient 分配给 WebView.

You can use a Javascript interface for the WebView to return the entirety of the HTML source when the page is finished loading. To do this, you'll need to assign your own WebViewClient to the WebView.

为此,请在您的 Activity 类中使​​用类似于以下内容的内容 -- 确保您的 Activity 实现 Observer:

To do this, use something similar to the following in your Activity class -- Make sure your Activity implements Observer:

public void onCreate(Bundle savedInstanceState) {
    // ...

    webView.setWebViewClient(new MyWebViewClient());
    HtmlJSInterface htmlJSInterface = new HtmlJSInterface();
    webView.addJavascriptInterface(htmlJSInterface, "HTMLOUT");
    htmlJSInterface.addObserver(this);

    // ...
}

// Called when our JavaScript Interface Observables are updated.
@Override
public void update(Observable observable, Object observation) {

    // Got full page source.
    if (observable instanceof HtmlJSInterface) {
        html = (String) observation;
        onHtmlChanged();
    }
}

private void onHtmlChanged() {
    // Do stuff here...
}

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        // When each page is finished we're going to inject our custom
        // JavaScript which allows us to
        // communicate to the JS Interfaces. Responsible for sending full
        // HTML over to the
        // HtmlJSInterface...
        isStarted = false;
        isLoaded = true;
        timeoutTimer.cancel();
        view.loadUrl("javascript:(function() { "
                + "window.HTMLOUT.setHtml('<html>'+"
                + "document.getElementsByTagName('html')[0].innerHTML+'</html>');})();");
        }
    }
}

然后,您将要创建 HtmlJSInterface 类,如下所示:

Then, you're going to want to create the HtmlJSInterface class, as such:

   public class HtmlJSInterface extends Observable {
  private String html;

  /**
   * @return The most recent HTML received by the interface
   */
  public String getHtml() {
    return this.html;
  }

  /**
   * Sets most recent HTML and notifies observers.
   * 
   * @param html
   *          The full HTML of a page
   */
  public void setHtml(String html) {
    this.html = html;
    setChanged();
    notifyObservers(html);
  }
}

这篇关于将 webview 内容保存到 android 存储并加载它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆