I created an application with webview. if I have any actions and the network is disconnected, I want to display one warning. I tried the following,
added this to the oncreate method.
public class AndroidNetTestActivity extends Activity { public static WebView webview; private Handler mHandler = new Handler(); private boolean isConnected = true; final String offlineMessageHtml = "Net is disconnected"; final String timeoutMessageHtml = "Connection timed out"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); webview=(WebView)findViewById(R.id.webview); webview.getSettings().setJavaScriptEnabled(true); webview.loadUrl("file:///android_asset/www/index.htm"); webview.addJavascriptInterface(new MyJavaScriptInterface(), "Android"); isConnected=isNetworkAvailable(); webview.setNetworkAvailable(isConnected); webview.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { isConnected=isNetworkAvailable(); if (isConnected) {
if I press the login button, it should show an error message if the network is unavailable.
but it does not work. please check my code and tell me what i did wrong
source share