I've been working on this for a couple of days now. I just started working with Android. I have a 2 part question. i want to check if internet is available. If it is launch the webView if not give an alert and prevent webView from loading. I have tried this but it force closes if there is not internet connection(e.g. airplane mode). How do I get it to give a message and stop loading the webview. Also is there a way to continuously check for connection so that the webpage 404 is not shown when it loses connection?
Here is my code.
public class MainActivity extends Activity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
android.net.NetworkInfo wifi = cm
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo datac = cm
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if ((wifi != null & datac != null)
&& (wifi.isConnected() | datac.isConnected())) {
setContentView(R.layout.activity_main);
}else{
//no connection
Toast toast = Toast.makeText(MainActivity.this, "No Internet Connection", Toast.LENGTH_LONG);
toast.show();
}
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(new MyWebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://mysite.com/?ma=1");
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("mysite.com")) {
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.logout:
finish();
return true;
}
return false;
}
}
Edit: Added Errors To be specific, I get these errors when pressing back after it shows the Toast saying there is no internet connect. It crashes if I use the back button after the toast message and the errors are below.
04-07 14:40:38.664: E/AndroidRuntime(15173): FATAL EXCEPTION: main
04-07 14:40:38.664: E/AndroidRuntime(15173): java.lang.NullPointerException
04-07 14:40:38.664: E/AndroidRuntime(15173): at com.mysite.testapp.MainActivity.onKeyDown(MainActivity.java:67)
04-07 14:40:38.664: E/AndroidRuntime(15173): at android.view.KeyEvent.dispatch(KeyEvent.java:2705)
04-07 14:40:38.664: E/AndroidRuntime(15173): at android.app.Activity.dispatchKeyEvent(Activity.java:2401)
04-07 14:40:38.664: E/AndroidRuntime(15173): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2007)
04-07 14:40:38.664: E/AndroidRuntime(15173): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3813)
04-07 14:40:38.664: E/AndroidRuntime(15173): at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3761)
04-07 14:40:38.664: E/AndroidRuntime(15173): at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2926)
04-07 14:40:38.664: E/AndroidRuntime(15173): at android.os.Handler.dispatchMessage(Handler.java:99)
04-07 14:40:38.664: E/AndroidRuntime(15173): at android.os.Looper.loop(Looper.java:137)
04-07 14:40:38.664: E/AndroidRuntime(15173): at android.app.ActivityThread.main(ActivityThread.java:4918)
04-07 14:40:38.664: E/AndroidRuntime(15173): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 14:40:38.664: E/AndroidRuntime(15173): at java.lang.reflect.Method.invoke(Method.java:511)
04-07 14:40:38.664: E/AndroidRuntime(15173): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
04-07 14:40:38.664: E/AndroidRuntime(15173): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
04-07 14:40:38.664: E/AndroidRuntime(15173): at dalvik.system.NativeStart.main(Native Method)