When net connection is on show a toast automatic

36 Views Asked by At

I want to created an app.if data connection is on then show a toast automatic. Otherwise not show. Im created many apps but i don't know how to make this. I try this but not work.

public class MainActivity extends Activity {

private WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.main);
    webView = (WebView) findViewById(R.id.wv);
    if(CheckNetwork.isInternetAvailable(MainActivity.this))
    {
        CookieManager cookieManager = CookieManager.getInstance(); 
        cookieManager.setAcceptCookie(true); 

        // webView = (WebView) findViewById(R.id.wv);
        // webView.setWebViewClient(new MyWebViewClient());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient() {
                public void onProgressChanged(WebView view, int progress) {
                    // Activities and WebViews measure progress with different scales.
                    // The progress meter will automatically disappear when we reach 100%
                    MainActivity.this.setProgress(progress * 1000);
                }
            });
        webView.setWebViewClient(new WebViewClient() {
                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                    Toast.makeText(MainActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show();
                }
            });

        webView.loadUrl("http://slashdot.org/");
    }
    else{
        //no connection
        Toast toast = Toast.makeText(MainActivity.this, "No Internet Connection", Toast.LENGTH_LONG);
        toast.show();  
    }


}   



@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
        webView.goBack();
        return true;
    }
    else
    {
        finish();
    }
    return super.onKeyDown(keyCode, event);
}

}

1

There are 1 best solutions below

0
On

Here is code wether internet reachable to host:

 //===================== INTERNET CHECK===========
        public boolean isConnectingToInternet(Context context) {
            boolean connected = false;
            ConnectivityManager CManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo NInfo = CManager.getActiveNetworkInfo();
            if (NInfo != null && NInfo.isConnectedOrConnecting()) {
                try {
                    if (InetAddress.getByName("www.rameshkhatri.com").isReachable(7000)) { // you change to your website name
                        // host reachable
                        connected = true;
                    } else {
                        connected = true;
    //                    connected = false;
                        // host not reachable
                    }
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return connected;
        }

But you have check internet connection in AsyncTask because network call will not be on UIThread, otherwise you get exception. Hope this helps. feel free to ask if you have queries