简体   繁体   中英

Loading html from assets if no internet connection

I am trying to make a webview where the app loads a HTML page from assets if there is no internet connection. I have been following this QUESTION and i have made a CheckNetwork.java but where shall i put this code below into my MainActivity ? I am a beginner at this so please explain as simple as possible.

if(CheckNetwork.isInternetAvailable(MainActivity.this)) 
  {
   // do something
  }

This is my MainActivity

public class MainActivity extends ActionBarActivity {

WebView browser;
private ourViewClient mClass;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mClass = new ourViewClient(this);

browser = (WebView) findViewById(R.id.wvwMain);

browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setLoadWithOverviewMode(true);
browser.getSettings().setUseWideViewPort(true);

browser.setWebViewClient(new ourViewClient(this));
try {
    browser.loadUrl("http://MyWebPage");
}

catch (Exception e) {
    e.printStackTrace();
   }

  }

} 

This should do the trick for you:

if(CheckNetwork.isInternetAvailable(MainActivity.this)) 
   browser.loadUrl("http://MyWebPage");
} else {
   browser.loadUrl("file:///android_asset/your_html.html");
}

I am also including example of how you load a html from your assets.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM