我想制作一个具有 webview 布局的 android 应用程序。这是我的申请标准:
应用程序第一次启动时,webview 加载一个 url(可能是 facebook、google 等..) webview.loadUrl("http://www.google.com");
加载 url 后,应用程序将加载的 url 保存到 Android 内部存储中“特定位置”的 HTML 视图(file.htm)中。所以,假设我打开“google.com”,应用程序将 google 的网页保存到 HTML 文件(假设文件名“google.htm”),当我转到该“特定位置”并单击“google .htm”文件,它使用 android 的 HTML 查看器显示 google 网页。
当应用程序再次启动时,或者简单地说应用程序再次加载网址(在本例中为“google.com”)时,它不会从“google.com”页面获取,而是从“google.com”页面获取。 htm”文件位于内部存储 android.htm 中。因此,从用户的角度来看,该应用程序仍然可以加载网页,即使它没有连接到互联网。
为了简单起见,
有人可以帮我提供代码和解释吗?我真的很感激。谢谢大家:D
您可以使用 WebView 的 Javascript 接口在页面加载完成时返回整个 HTML 源。为此,您需要将自己的 WebViewClient 分配给 WebView。
为此,请在您的 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>');})();");
}
}
}
然后,您需要创建 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);
}
}
另一种解决方案是使用
saveWebArchive
private fun offlineSave(webView1:WebView, webView2:WebView) {
webView1.loadUrl("https://example.com/privacy")
val settings = webView1.settings
settings.javaScriptEnabled = true
settings.domStorageEnabled = true
settings.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK
settings.setDatabaseEnabled(true);
settings.setDomStorageEnabled(true);
settings.setAllowContentAccess(true);
settings.setAllowFileAccess(true);
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
Handler().postDelayed({//you can do onPageFinished
val imageFile = File(filesDir, "doc.mht")
webView1.saveWebArchive(imageFile.path, false, ValueCallback<String?> {
Log.e("***** saved", "saved")
runOnUiThread {
val settings = webView2.settings
settings.javaScriptEnabled = true
settings.domStorageEnabled = true
settings.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK
settings.setDatabaseEnabled(true)
settings.setDomStorageEnabled(true)
settings.setAllowContentAccess(true)
settings.setAllowFileAccess(true)
settings.setJavaScriptEnabled(true)
settings.setJavaScriptCanOpenWindowsAutomatically(true)
webView2.loadUrl("file://" + imageFile.path)
}
})
}, 4000)
runOnUiThread {
val imageFile = File(filesDir, "doc.mht")
val settings = webView2.settings
settings.javaScriptEnabled = true
settings.domStorageEnabled = true
settings.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK
settings.setDatabaseEnabled(true)
settings.setDomStorageEnabled(true)
settings.setAllowContentAccess(true)
settings.setAllowFileAccess(true)
settings.setJavaScriptEnabled(true)
settings.setJavaScriptCanOpenWindowsAutomatically(true)
webView2.loadUrl("file://" + imageFile.path)
}
}