保存的WebView内容到Android存储和加载加载、内容、WebView、Android

2023-09-06 01:28:37 作者:狂野男孩

我想打一个Android应用程序有一个web视图布局。这是我的应用程序的标准:

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

第一次应用程序启动后的WebView加载一个URL(也许Facebook,谷歌等。) webview.loadUrl(http://www.google.com);

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

在它加载的URL,在应用程序保存加载的URL到HTML视图(file.htm)在一个特殊的地方,在Android的内部存储。所以,让我们说我打开google.com,该应用程序保存谷歌的网页HTML文件(假设文件名,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.

当应用程序重新开始,或简单地说,应用程序再次加载链接(在此情况下,google.com),它不从google.com页取,但它需要从在内部存储机器人的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.

要使它简单,

在应用程序启动 - >转到指定的URL - >检查存储 如果有指定的URL已在存储器中的HTML文件,然后从存储负载 ELSE它加载的URL从网络。

谁能帮我用code和解释?我真的AP preciate它。感谢球员:D

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

推荐答案

您可以使用JavaScript接口,用于web视图返回页面时完成加载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类的东西 - 确保您的活动实现了观察

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);
  }
}
 
精彩推荐
图片推荐