If the network adapter is switched off then the webview should load from cache. This part is not working. I have followed the tutorial below and taken parts from it in a bit to save the website pages in a cache saved on the sdcard.
If i run the app the web site is displayed. If i then turn off the network adapter and visit the site, it will not display. I check the cache directory on the sdcard and parts of the site have been written to the cache. Why doesn't the site load from cache when off-line?
http://www.devahead.com/blog/
The tutorial shows you how to create a directory on the sdcard and write to it. The directories do exist but the webview is not restored from this cache in offline mode.
below is how i call the mainactivity that displays the webview.
Using java Syntax Highlighting
- Intent i = new Intent(getApplicationContext(), MainActivity.class);
- //i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- i.putExtra("name", "hardcoded client name");
- i.putExtra("email", "hardcoded email");
- startActivity(i);
- finish();
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
Here is the full MainActivity. You can ignore the first part as that's the code that handle the GCM push notification stuff, that all works. I need to know why the webview will not load from cache.
Using java Syntax Highlighting
- import static com.bmi.bmitestapp.CommonUtilities.DISPLAY_MESSAGE_ACTION;
- import static com.bmi.bmitestapp.CommonUtilities.EXTRA_MESSAGE;
- import static com.bmi.bmitestapp.CommonUtilities.SENDER_ID;
- import java.io.File;
- import android.app.Activity;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.util.Log;
- import android.webkit.WebChromeClient;
- import android.webkit.WebSettings;
- import android.webkit.WebView;
- import android.webkit.WebViewClient;
- import android.widget.TextView;
- import android.widget.Toast;
- import android.webkit.*;
- import com.google.android.gcm.GCMRegistrar;
- public class MainActivity extends Activity {
- private static final String TAG = MainActivity.class.getSimpleName();
- // label to display gcm messages
- TextView lblMessage;
- WebView webView;
- // Asyntask
- AsyncTask<Void, Void, Void> mRegisterTask;
- // Alert dialog manager
- AlertDialogManager alert = new AlertDialogManager();
- // Connection detector
- ConnectionDetector cd;
- public static String name;
- public static String email;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Log.e(TAG, "in onCreate in mainactivity");
- cd = new ConnectionDetector(getApplicationContext());
- // Check if Internet present
- // if (!cd.isConnectingToInternet()) {
- // // Internet Connection is not present
- // alert.showAlertDialog(MainActivity.this,
- // "Internet Connection Error",
- // "Please connect to working Internet connection", false);
- // // stop executing code by return
- // return;
- // }
- // Getting name, email from intent
- Intent i = getIntent();
- name = i.getStringExtra("name");
- email = i.getStringExtra("email");
- // Make sure the device has the proper dependencies.
- GCMRegistrar.checkDevice(this);
- // Make sure the manifest was properly set - comment out this line
- // while developing the app, then uncomment it when it's ready.
- GCMRegistrar.checkManifest(this);
- lblMessage = (TextView) findViewById(R.id.lblMessage);
- registerReceiver(mHandleMessageReceiver, new IntentFilter(
- DISPLAY_MESSAGE_ACTION));
- // Get GCM registration id
- final String regId = GCMRegistrar.getRegistrationId(this);
- // Check if regid already presents
- if (regId.equals("")) {
- // Registration is not present, register now with GCM
- GCMRegistrar.register(this, SENDER_ID);
- } else {
- // Device is already registered on GCM
- if (GCMRegistrar.isRegisteredOnServer(this)) {
- // Skips registration.
- Toast.makeText(getApplicationContext(), "Already registered with GCM", Toast.LENGTH_LONG).show();
- } else {
- // Try to register again, but not in the UI thread.
- // It's also necessary to cancel the thread onDestroy(),
- // hence the use of AsyncTask instead of a raw thread.
- final Context context = this;
- mRegisterTask = new AsyncTask<Void, Void, Void>() {
- @Override
- protected Void doInBackground(Void... params) {
- // Register on our server
- // On server creates a new user
- ServerUtilities.register(context, name, email, regId);
- return null;
- }
- @Override
- protected void onPostExecute(Void result) {
- mRegisterTask = null;
- }
- };
- mRegisterTask.execute(null, null, null);
- }
- }
- webView = (WebView)findViewById(R.id.webView1);
- // Initialize the WebView
- webView.getSettings().setSupportZoom(true);
- webView.getSettings().setBuiltInZoomControls(true);
- webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
- webView.setScrollbarFadingEnabled(true);
- webView.getSettings().setLoadsImagesAutomatically(true);
- webView.getSettings().setDomStorageEnabled(true);
- // Set cache size to 8 mb by default. should be more than enough
- webView.getSettings().setAppCacheMaxSize(1024*1024*8);
- // This next one is crazy. It's the DEFAULT location for your app's cache
- // But it didn't work for me without this line.
- // UPDATE: no hardcoded path. Thanks to Kevin Hawkins
- String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
- Log.e(TAG, "appCachePath = " + appCachePath);
- webView.getSettings().setAppCachePath(appCachePath);
- webView.getSettings().setAllowFileAccess(true);
- webView.getSettings().setAppCacheEnabled(true);
- // Load the URLs inside the WebView, not in the external web browser
- webView.setWebViewClient(new WebViewClient());
- if (savedInstanceState == null)
- {
- if(isNetworkAvailable() == true){
- Log.e(TAG, "we have a network connection");
- webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
- webView.loadUrl("http://bmi.cubecore.co.uk");
- } else {
- Log.e(TAG, "we don't have a network connection");
- webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ONLY);
- webView.loadUrl("http://bmi.cubecore.co.uk");
- }
- }
- } //end of oncreate
- private boolean isNetworkAvailable() {
- ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
- return activeNetworkInfo != null;
- }
- /**
- * Receiving push messages
- * */
- private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
- // Waking up mobile if it is sleeping
- WakeLocker.acquire(getApplicationContext());
- /**
- * Take appropriate action on this message
- * depending upon your app requirement
- * For now i am just displaying it on the screen
- * */
- // Showing received message
- lblMessage.append(newMessage + "\n");
- Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show();
- // Releasing wake lock
- WakeLocker.release();
- }
- };
- @Override
- protected void onDestroy() {
- super.onDestroy();
- // Clear the cache (this clears the WebViews cache for the entire application)
- //webView.clearCache(false);
- if (mRegisterTask != null) {
- mRegisterTask.cancel(true);
- }
- try {
- unregisterReceiver(mHandleMessageReceiver);
- GCMRegistrar.onDestroy(this);
- } catch (Exception e) {
- Log.e("UnRegister Receiver Error", "> " + e.getMessage());
- }
- }
- @Override
- public void onBackPressed() {
- super.onBackPressed();
- }
- @Override
- protected void onResume() {
- super.onResume();
- Log.e(TAG, "in onResume in mainactivity");
- }
- @Override
- public File getCacheDir()
- {
- // NOTE: this method is used in Android 2.1
- Log.e(TAG, "getcachedir");
- return getApplicationContext().getCacheDir();
- }
- @Override
- protected void onSaveInstanceState(Bundle outState)
- {
- super.onSaveInstanceState(outState);
- // Save the state of the WebView
- webView.saveState(outState);
- }
- @Override
- protected void onRestoreInstanceState(Bundle savedInstanceState)
- {
- super.onRestoreInstanceState(savedInstanceState);
- // Restore the state of the WebView
- webView.restoreState(savedInstanceState);
- }
- }
Parsed in 0.062 seconds, using GeSHi 1.0.8.4
Here's the code from the tutorial above that writes to the sdcard.
Using java Syntax Highlighting
- package com.bmi.bmitestapp;
- import java.io.File;
- import android.app.Application;
- import android.os.Environment;
- import android.util.Log;
- public class ApplicationExt extends Application
- {
- private static final String TAG = ApplicationExt.class.getSimpleName();
- // NOTE: the content of this path will be deleted
- // when the application is uninstalled (Android 2.2 and higher)
- protected File extStorageAppBasePath;
- protected File extStorageAppCachePath;
- @Override
- public void onCreate()
- {
- super.onCreate();
- Log.e(TAG, "inside appext");
- // Check if the external storage is writeable
- if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
- {
- // Retrieve the base path for the application in the external storage
- File externalStorageDir = Environment.getExternalStorageDirectory();
- if (externalStorageDir != null)
- {
- // {SD_PATH}/Android/data/com.devahead.androidwebviewcacheonsd
- extStorageAppBasePath = new File(externalStorageDir.getAbsolutePath() +
- File.separator + "Android" + File.separator + "data" +
- File.separator + getPackageName());
- }
- if (extStorageAppBasePath != null)
- {
- // {SD_PATH}/Android/data/com.devahead.androidwebviewcacheonsd/cache
- extStorageAppCachePath = new File(extStorageAppBasePath.getAbsolutePath() +
- File.separator + "cache");
- boolean isCachePathAvailable = true;
- if (!extStorageAppCachePath.exists())
- {
- // Create the cache path on the external storage
- isCachePathAvailable = extStorageAppCachePath.mkdirs();
- }
- if (!isCachePathAvailable)
- {
- // Unable to create the cache path
- extStorageAppCachePath = null;
- }
- }
- }
- }
- @Override
- public File getCacheDir()
- {
- // NOTE: this method is used in Android 2.2 and higher
- if (extStorageAppCachePath != null)
- {
- // Use the external storage for the cache
- Log.e(TAG, "extStorageAppCachePath = " + extStorageAppCachePath);
- return extStorageAppCachePath;
- }
- else
- {
- // /data/data/com.devahead.androidwebviewcacheonsd/cache
- return super.getCacheDir();
- }
- }
- }
- android caching webview
Parsed in 0.042 seconds, using GeSHi 1.0.8.4

