0

I would like to make an custom html page to load when no internet connection. I don't want to use the regular one so can someone fill in what's missing? I'm pretty new to this, so I followed instructions. If anything else is needed like permission, please tell me. This is my code:

MainActivity:

package com.example.app;

public class MainActivity extends ActionBarActivity {

private WebView mWebView;

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

    mWebView = (WebView) findViewById(R.id.activity_main_webview);
        mWebView.loadUrl("http://domain.com");

    // Force links and redirects to open in the WebView instead of in a browser
    mWebView.setWebViewClient(new WebViewClient());
    // Stop local links and redirects from opening in browser instead of WebView
    mWebView.setWebViewClient(new MyAppWebViewClient());

    mWebView = (WebView) findViewById(R.id.activity_main_webview);
    mWebView.getSettings().setLoadWithOverviewMode(true);
    mWebView.getSettings().setUseWideViewPort(true);
    mWebView.setInitialScale(1);
}

@Override
// Detect when the back button is pressed
public void onBackPressed() {
    if(mWebView.canGoBack()) {
        mWebView.goBack();
    } else {
        // Let the system handle the back button
        super.onBackPressed();
    }
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }
}

}
1

1 Answer 1

6

check whether internet is connected or not..

   private boolean haveNetworkConnection() {
      boolean haveConnectedWifi = false;
      boolean haveConnectedMobile = false;

      ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo[] netInfo = cm.getAllNetworkInfo();

  for (NetworkInfo ni : netInfo) {
     if (ni.getTypeName().equalsIgnoreCase("WIFI"))
        if (ni.isConnected())
            haveConnectedWifi = true;
    if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
        if (ni.isConnected())
            haveConnectedMobile = true;
}
return haveConnectedWifi || haveConnectedMobile;
}

now while loading your page check the condition

    if(haveNetworkConnection()){
        mWebView.loadUrl("http://domain.com");
   } else {
    mWebView.loadUrl("file:///android_asset/custom.html");
   }

add this permisson to your manifest file..

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
2
  • It says Cannot resolve symbol (haveNetworkConnection), I know it may be simple, but I am realy new to this. I don't know if I have to make new class or put the code in the this. I use Android Studio.
    – Ivan
    Mar 6, 2014 at 19:59
  • oops sorry i forget to put braces ...try updated one
    – Apurv
    Mar 7, 2014 at 5:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.