Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to pre-cache a web page for viewing with an Android WebView?

I've read about the HTML5 cache manifest, and I've seen Android does support caching websites using the cache manifest. I want to use the cache manifest to download all the required resources for my website to preload it, and then open a WebView and display the remote website using the pre-cached resources. I want to pre-cache my remote page somehow, preferably without using a WebView for the caching process.

The problem with using a WebView for the pre-caching process is that loading the webpage using a WebView renders and executes the page instead of just downloading it.

I've read this: http://alex.tapmania.org/2010/11/html5-cache-android-webview.html, but having a WebView support caching is not what I want. I want the loading process to be instantaneous (assuming the cache manifest / etags of the remote website are the same as the cached version) right after I finish pre-caching the resources, instead of waiting for the WebView to load up and cache everything on the first access.

What is the correct way of pre-caching web pages for viewing later?

Thanks!

like image 493
Ron Reiter Avatar asked Oct 02 '11 13:10

Ron Reiter


People also ask

Can we cache WebView in android?

You can use the WebView cache to enable caching in WebView.

Does WebView have cache?

WebViews are android views, that when created, consume context . And if we cache activity context on the application level, we might end up leaking the activity that was used to create the web-view.

How do I enable cache in WebView?

This example demonstrate about How to enable app cache for webview in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

Which view displays web pages in android?

The WebView class is an extension of Android's View class that allows you to display web pages as a part of your activity layout. It does not include any features of a fully developed web browser, such as navigation controls or an address bar. All that WebView does, by default, is show a web page.


1 Answers

What you want to do requires implementing a mechanism for app cache, linked resources, cookies, and local database store for HTML5 apps that use database API, and that's an important part of what browsers do in these days. I don't recommend doing the caching by yourself, not only because it's so much work, but also because I can't recall any method in WebView and it's friends (WebViewClient, etc.) that accepts an outside cache.

But your problem has a very simpler solution: you can put a WebView in your view and set its visibility to gone. Then make it visible when it has finished loading the page. WebView also automatically keeps the cache for your app so that the next time it runs it loads the page more quickly.

For hiding your WebView and then automatically showing it you just have to override onPageFinished in WebViewClient.

like image 107
Mostafa Avatar answered Sep 23 '22 15:09

Mostafa