Check your internet connection at ANDROID

Hello, I have an application based on data received over the Internet ...

How can I process my application if there is no connection?

I can detect a connection with

ConnectivityManager cm = (ConnectivityManager) 
getSystemService(this.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo().isConnectedOrConnecting();

It works great ... But how can I prevent Force Erros when I know there is no connection? I would like the message to be shown - something like: "Sorry! No internet connection available!" not my crushing app ...

+4
source share
6 answers
 /**
   * 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;
  }
+32
source

getActiveNetworkInfo () can return null, so you get strength, but you can do this:

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();
}
+4
source

, , NetworkInfo.isConnectedOrConnecting(); , , , , , UnknownHostException.
catch catch try catch

catch (UnknownHostException e) {
    getRequest.abort();
    Log.w("unknownhostexception whicle connects to the host " + url, e);
    }

- , .

+3

.

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
+1
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;
}
0
ConnectivityManager conMgr  = ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); 
 NetworkInfo info = conMgr.getActiveNetworkInfo(); 

if(info != null && info.isConnected()) 
{
   // internet is there.
}
else
{
   // internet is not there.
}
0
source

All Articles