Reputation: 110
I know the code how to check the Internet connection in the device but i want to how to manage the internet connection check as my app contain many asnchronours tasks, services etc. means where to show the Internet Error Dialog.
Upvotes: 0
Views: 216
Reputation: 5543
Create a Utility class where add a method to check for the internet availability as follows :
/**
* method to check the connection
*
* @return true if network connection is available
*/
public static boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) MainApplication.getsApplicationContext().getSystemService(
Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if (info == null)
return false;
State network = info.getState();
return (network == State.CONNECTED || network == State.CONNECTING);
}
/**
* method to toast message if internet is not connected
*
* @return true if internet is connected
*/
public static boolean isInternetConnected() {
if (isNetworkAvailable()) {
return true;
} else {
// TODO Here write the code to show dialog with Internet Not Available message
......
return false;
}
}
And now you can use it before you execute your AsyncTask just add this check
if (Utility.isInternetConnected()) {
// TODO start your asyntask here
}
Upvotes: 0
Reputation: 1215
You can register for the ConnectivityManager.CONNECTIVITY_ACTION broadcast action to listen for the connectivity changes on the device.
ConnectivityManager.CONNECTIVITY_ACTION http://developer.android.com/reference/android/net/ConnectivityManager.html#CONNECTIVITY_ACTION
Upvotes: 1