Q:

android checking internet connection when a button is pressed

i ask the same question previously but could not find a proper solution.my problem is that i want to show an messege if there is no internet connection if the web view loads and also at the same time if i press the refresh button in the android action bar..it is not displaying the toast if  i press the refresh button.can someone plse help me??
this is my connection class
public class Datacon extends Activity {
    public static boolean checkInternetConnection(Context context) {

    ConnectivityManager con_manager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    if (con_manager.getActiveNetworkInfo() != null
            && con_manager.getActiveNetworkInfo().isAvailable()
            && con_manager.getActiveNetworkInfo().isConnected()) {
        return true;
    } else {
        return false;
    }
}

}
this is mywebview.java
public class Mywebpage<Bitmap> extends Activity {
 String url;
  WebView myWebView ;

  ProgressBar loadingProgressBar,loadingTitle;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
final Context context = this;
setContentView(R.layout.webviewpage);
AdView ads=(AdView)this.findViewById(R.id.adViewer);
AdRequest request=new AdRequest.Builder().build();
ads.loadAd(request);
View mActionBarView = getLayoutInflater().inflate(R.layout.my_actionbar, null);
final android.app.ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
getActionBar().setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent))); 
actionBar.setCustomView(mActionBarView);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.gradientwhite));
Button refresh = (Button) actionBar.getCustomView().findViewById(R.id.but2);

Button search = (Button) actionBar.getCustomView().findViewById(R.id.button1);
search.setOnClickListener(new OnClickListener() {
    public void onClick(View arg0) {
Intent inte=new Intent(Mywebpage.this,MainActivity.class);
inte.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(inte);
    finish();
    }
    })    ;

Bundle extras = getIntent().getExtras();
String title;

if (!Datacon.checkInternetConnection(this)) {

    for (int i=0; i < 3; i++)
    {
          Toast.makeText(this, "Check your Internet Connection!", Toast.LENGTH_LONG).show();
    }

} else {

if (extras != null) {
    title = extras.getString("title");
    url = extras.getString("url");
    TextView text=(TextView) findViewById(R.id.textView1);
    text.setText(title);
    myWebView=(WebView)findViewById(R.id.WebView);
    myWebView.loadUrl(url);
    myWebView.getSettings().setLoadWithOverviewMode(true);
    myWebView.getSettings().setUseWideViewPort(true);
    myWebView.getSettings().setBuiltInZoomControls(true);
    myWebView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN); 
    loadingProgressBar=(ProgressBar)findViewById(R.id.progressBar1); 

    myWebView.setWebViewClient(new WebViewClient(){

        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            loadingProgressBar.setVisibility(View.VISIBLE);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            loadingProgressBar.setVisibility(View.GONE);
        }
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return false;
        }
    });

    refresh.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            ConnectivityManager con_manager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo netInfo = con_manager
                    .getActiveNetworkInfo();
            String status = netInfo.getState().toString();
            if (status.equalsIgnoreCase("CONNECTED")) {
                myWebView.loadUrl(url);

            }

            else  {

                Context context_new = getApplicationContext();
                CharSequence text = "Hello n!";
                int duration = Toast.LENGTH_LONG;
                Toast toast = Toast.makeText(context_new, text,
                        duration);
                toast.show();

            }

        }
    });

    }
}
}
}

it is not showing the toast messege when refresh button is pressed if there is no internet connection..

A:

Write your refresh button click event like below:
refresh.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (Datacon.checkInternetConnection(Mywebpage.this)) {
                myWebView.loadUrl(url);

            }

            else  {

                Context context_new = getApplicationContext();
                CharSequence text = "Hello n!";
                int duration = Toast.LENGTH_LONG;
                Toast toast = Toast.makeText(context_new, text,
                        duration);
                toast.show();

            }

        }
    });

