This question already has an answer here:
-
How to check my internet access on Android?
5 answers
Hello I have application based on data retrieved over internet...
How can I handle my app in case there is no connection available?
I can detect connection with
ConnectivityManager cm = (ConnectivityManager)
getSystemService(this.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo().isConnectedOrConnecting();
It works fine... But how can I prevent Force Erros when I know there is no connection? I would like a message to be shown - something like: "Sorry! There is no internet connection available!" and not my application to crush...
/**
* Checks if the device has Internet connection.
*
* @return <code>true</code> if the phone is connected to the Internet.
*/
public static boolean hasConnection() {
ConnectivityManager cm = (ConnectivityManager) MbridgeApp.getContext().getSystemService(
Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifiNetwork != null && wifiNetwork.isConnected()) {
return true;
}
NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mobileNetwork != null && mobileNetwork.isConnected()) {
return true;
}
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnected()) {
return true;
}
return false;
}
getActiveNetworkInfo() may return null, so you will get a force close, but you can do that:
ConnectivityManager cm = (ConnectivityManager)
getSystemService(this.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (null == ni)
return false;
return ni.isConnectedOrConnecting();
Then the check is simple:
if (networkAvailable()) // << your method from above
{
// Do stuff
}
else
{
Toast.makeToast(yourcontext, "No network available", Toast.LENGTH_LONG).show();
}
I can't make comment but note that NetworkInfo.isConnectedOrConnecting();
in all the above answers will only tell you if you are connected to router or not but if you are connected to router that don't have internet access then your application will crash because it will through UnknownHostException.
you should add the following catch enclose to your try catch
catch (UnknownHostException e) {
getRequest.abort();
Log.w("unknownhostexception whicle connects to the host " + url, e);
}
or make timeout for your request what ever you want.
For this you need to have following permission in your manifest.
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
private boolean connectionAvailable() {
boolean connected = false;
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {
//we are connected to a network
connected = true;
}
return connected;
}
ConnectivityManager conMgr = ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = conMgr.getActiveNetworkInfo();
if(info != null && info.isConnected())
{
// internet is there.
}
else
{
// internet is not there.
}