简体   繁体   English

将Webview内容保存到android存储并加载

[英]Save webview content to android storage and load it

I want to make an android application which has a webview layout. 我想制作一个具有webview布局的android应用程序。 This is the criteria of my application: 这是我的申请标准:

  1. The first time the application starts, webview loads an url (maybe facebook, google, etc..) webview.loadUrl("http://www.google.com"); 该应用程序第一次启动时,Webview会加载一个URL(也许是Facebook,Google等)。webview.loadUrl(“ http://www.google.com”);

  2. 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. 加载网址后,应用程序会将加载的网址保存到android内部存储器中“特定位置”的HTML视图(file.htm)中。 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. 因此,假设我打开“ google.com”,该应用程序将Google的网页保存为HTML文件(例如,文件名“ google.htm”),然后当我转到该“特定位置”并单击“ google .htm”文件,它将使用android的HTML查看器显示Google网页。

  3. 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. 当应用程序再次启动时,或者只是说应用程序再次加载了url(在本例中为“ google.com”)时,它不会从“ google.com”页面中获取,但会从“ google.htm”中获取文件在内部存储的android上。 So from the user's view, that application can still load webpages, even though it's not connected to internet. 因此,从用户的角度来看,即使未连接到Internet,该应用程序仍可以加载网页。

To make it simple, 简单起见

  1. Application Start -> Go to the specified url -> Check the storage 应用程序开始->转到指定的URL->检查存储
  2. IF there's the specified url HAS the HTML file in the storage, then load from the storage 如果存在指定的URL,则存储中有HTML文件,然后从存储中加载
  3. ELSE it loads the url from the web. 否则,它将从Web加载URL。

Can anyone help me with the code and explanation? 谁能帮我提供代码和解释? I really appreciate it. 我真的很感激。 Thanks guys :D 谢谢大家:D

You can use a Javascript interface for the WebView to return the entirety of the HTML source when the page is finished loading. 您可以在WebView使用Javascript接口,以在页面加载完成后返回HTML源代码的全部。 To do this, you'll need to assign your own WebViewClient to the WebView. 为此,您需要将自己的WebViewClient分配给WebView。

To do this, use something similar to the following in your Activity class -- Make sure your Activity implements Observer : 为此,请在Activity类中使用类似于以下内容的内容- 确保您的Activity实现了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>');})();");
        }
    }
}

Then, you're going to want to create the HtmlJSInterface class, as such: 然后,您将要创建HtmlJSInterface类,如下所示:

   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);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM