1

In my UI i check the network availability in a button click listner

NewButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if(isInternetAvailable)
                {
                Intent intent = new Intent(FeaturedListActivity.this,
                        NewJewelsActivity.class);
                startActivity(intent);
                finish();
                }
                else
                {
                    MyAlertDialog.ShowAlertDialog(FeaturedListActivity.this, "", "Check Network Connection.", "OK");    
                }
            }
        });

I check network availability using the following code

public boolean isNetworkAvailable() {
          ConnectivityManager connectivityManager= (ConnectivityManager) getSystemService(getApplicationContext().CONNECTIVITY_SERVICE);
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
            return activeNetworkInfo != null;

Now my problem is when i start the app with the wifi in off state the condition will work and will show the "check network connection" dialog. And now i press the center button of the phone and turn on the wifi and coming back to the screen from where i left.Now the wifi is in ON state and it still shows "check network connection".If i restart the app it will not show the dialog.How can i solve this?

}
playmaker420
  • 1,527
  • 4
  • 27
  • 52

2 Answers2

3

You should use a broadcast receiver to get your app notified about the changes in the network state.

Dinesh Venkata
  • 1,087
  • 1
  • 9
  • 22
  • 1
    Thank you. This is what i wanted. I got some code snippet from this http://stackoverflow.com/questions/10350449/how-to-check-the-internet-connection-periodically-in-whole-application/10350511#10350511 – playmaker420 Oct 08 '12 at 09:17
  • 1
    You are welcome broadcast receivers are the best when you want to get notified about a broadcast service such as change in a network state. – Dinesh Venkata Oct 08 '12 at 09:19
1

When the user clicks on the OK button, you should close the dialog and direct him to the settings wireless and networks screen. You should do a startActivityForResult, and when the user comes to the Application again then onActivityResult() will be called where you can handle your code. In onActivityResult() you can again check if internet is there or not and if not there then simply display a toast. On the button click you can directly start the wireless settings screen using

 startActivityForResult(new Intent(Settings.ACTION_WIRELESS_SETTINGS),0);
Antrromet
  • 15,294
  • 10
  • 60
  • 75