Check the state of Internet Connection | Techbirds
Generally We are developing an application that will fetch the information from the Internet and display it to the user. But if internet is not connected or if it suddenly goes out after your internet check.. then user will see a blank screen.. and once he/she will connected to the internet then they have to either click on the Refresh button or reopen the application…
To make it more interactive.. what we can do is to automatically reload the data once internet is connected…
Steps to do it :
1. Define a Receiver in your Manifest file :
2. Create a Receiver class and put this code in your Receiver class :
if (!intent.getAction() .equals(WifiManager.NETWORK_STATE_CHANGED_ACTION) && !intent.getAction().equals( ConnectivityManager.CONNECTIVITY_ACTION) && !intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { return;
}
ConnectivityManager cm = ((ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE));
if (cm == null) { return;
}
// Now to check if we’re actually connected if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected()) { // Reload your data and Notify to the list
}
482 total views, 2 views today
Share this On