I am actually new to programming in Java but I have been following several solutions to my problem here but didn't find one that suits my case and I can't seem to get the code down correctly.
I would like to have a WebView
that opens an online page (for example Google) when the phone is online and open a local HTML page when the phone is offline.
At the same time though I want the phone to overwrite the local page when it is online so that the offline local page is always updated to the last time the phone was connected to the internet.
Any ideas on how this could be done? Some simple pointing to the right direction could help.
Thanks a lot.
The loadUrl() and loadData() methods of Android WebView class are used to load and display web page.
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.
I read about how to increase performance of WebView by implementing Caching web resources like JS, CSS and image files. You can also static resources in your native application, and by intercepting the Resource requests you can override the default behaviour of WebView.
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.
That sounds like a simple webview caching mechanism to me.
The following should do what you are looking for:
WebView webView = new WebView( context ); webView.getSettings().setAppCacheMaxSize( 5 * 1024 * 1024 ); // 5MB webView.getSettings().setAppCachePath( getApplicationContext().getCacheDir().getAbsolutePath() ); webView.getSettings().setAllowFileAccess( true ); webView.getSettings().setAppCacheEnabled( true ); webView.getSettings().setJavaScriptEnabled( true ); webView.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT ); // load online by default if ( !isNetworkAvailable() ) { // loading offline webView.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ELSE_NETWORK ); } webView.loadUrl( "http://www.google.com" );
The method isNetworkAvailable()
checks for an active network connection:
private boolean isNetworkAvailable() { ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService( CONNECTIVITY_SERVICE ); NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); return activeNetworkInfo != null && activeNetworkInfo.isConnected(); }
Finally, don't forget to add the following three permissions to your AndroidManifest.xml
:
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
here are occasions that a WebView cannot be cached natively. If the page header contains the following fields, the WebView will not be able to cache the contents from that page. Cache-Control: no-store, no-cache Pragma: no-cache
In this case, youwill have to modify the page property on the server to solve the caching problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With