I've searched this and I can't resolve the problem, look at this code checked on other examples:
public class EntryActivity extends Activity {
public Boolean isNetAvailable(Context con) {
try{
ConnectivityManager connectivityManager = (ConnectivityManager)
con.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobileInfo =connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifiInfo.isConnected() || mobileInfo.isConnected()) {
return true;
}
}
catch(Exception e){
e.printStackTrace();
}
return false;
this method works well when I call it in onCreate method, like this:
if (isNetAvailable(getApplicationContext())) {
Toast.makeText(EntryActivity.this,
"You have Internet Connection, wait a moment to sincronize" +
"Metar's stations", Toast.LENGTH_LONG)
.show();
Intent myIntent = new Intent(this, MainActivity.class);
startActivity(myIntent);
} else {
Toast.makeText(EntryActivity.this,
"You Do not have Internet Connection",
Toast.LENGTH_LONG).show();
EntryActivity.this.startActivity(new Intent(
Settings.ACTION_WIRELESS_SETTINGS));
}
This only works for the first Internet connection Check! the problem is, if Wifi is off the user should leaves the app or turns Wi-Fi on.But, if He turns it on, I don't have access to that change and I can't advance to my other activity. So, If in the beggining the internet connection status is down, i want keep checking if the User turns it On to advanve to other activity. What can I do?