0

I want to show alert that connection not available in my android application.

I am calling some rest request in my application. To check this network unavailability scenario I manually disconnect my wifi in my laptop (I am testing in simulator) .

Code where I call service is here

resp = client.execute(httpPostRequest);

and here I am catching exception

catch (IllegalStateException e) {
            //assertTrue(e.getLocalizedMessage(), false);
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            //assertTrue(e.getLocalizedMessage(), false);
            e.printStackTrace();
        } catch (IOException e) {
            //assertTrue(e.getLocalizedMessage(), false);
            e.printStackTrace();
        }

and when I get exception I try to get the status of the device network connection with this function

public boolean IsInternetConnectted()
    {
        ConnectivityManager conMgr =  (ConnectivityManager)this.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo i = conMgr.getActiveNetworkInfo();
        conMgr = null;
        if (i == null)
            return false;
        if (!i.isConnected())
            return false;
        if (!i.isAvailable())
            return false;
        return true;        
    }

even if wi-fi is disconnected my simulator return true from this function.

How to resolve this situaltion??

CocoNess
  • 4,213
  • 4
  • 26
  • 43
Ankit HTech
  • 1,863
  • 6
  • 31
  • 42

8 Answers8

4

use this snippet:

 // added as an instance method to an Activity
boolean isNetworkConnectionAvailable() {  
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getActiveNetworkInfo();     
    if (info == null) return false;
    State network = info.getState();
    return (network == NetworkInfo.State.CONNECTED || network == NetworkInfo.State.CONNECTING);
}     
2

Hello Please Check Following answer :

http://stackoverflow.com/questions/13217676/how-to-check-the-real-internet-connected-in-android/13217956#13217956

But I want to suggest you that it is recommended that you should test the connection code in real device rather Emulator. Because emulator some time not giving accurate output.

1

I Found that same error in my Emulator... But Try your Code on Mobile Device this code Run Perfectly and not coming any error in real Device...

Gautam Vasoya
  • 881
  • 1
  • 8
  • 16
0

try this

public static Boolean checknetwork(Context mContext) {

    NetworkInfo info = ((ConnectivityManager) mContext
            .getSystemService(Context.CONNECTIVITY_SERVICE))
            .getActiveNetworkInfo();
    if (info == null || !info.isConnected()) 
    {

        return false;
    }
    if (info.isRoaming()) {
        // here is the roaming option you can change it if you want to
        // disable internet while roaming, just return false
        return true;
    }

    return true;

}
Sanket Kachhela
  • 10,861
  • 8
  • 50
  • 75
0

Try this:

public static boolean isOnline(Context context) {
        boolean state=false;
        ConnectivityManager cm = (ConnectivityManager)
        context.getSystemService(Context.CONNECTIVITY_SERVICE);


        NetworkInfo wifiNetwork =
            cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            if (wifiNetwork != null) {
            state=wifiNetwork.isConnectedOrConnecting();
             }

            NetworkInfo mobileNetwork =
            cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (mobileNetwork != null) {
            state=mobileNetwork.isConnectedOrConnecting();
            }

            NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
            if (activeNetwork != null) {
            state=activeNetwork.isConnectedOrConnecting();

            }

            return state;
      }

For Better Results, Check on Real Device..

Lokesh
  • 5,180
  • 4
  • 27
  • 42
0

Send HttpRequest and check then response code you are getting if its 200 you are connected to internet else you are not and can notify the user accordingly

saum22
  • 884
  • 12
  • 28
0

To Check Internet Connectivity

 public class CheckNetwork {


private static final String TAG = CheckNetwork.class.getSimpleName();



public static boolean isInternetAvailable(Context context)
{
    NetworkInfo info = (NetworkInfo) ((ConnectivityManager)
    context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();

    if (info == null)
    {
         Log.d(TAG,"no internet connection");
         //Toast.makeText(context, "No Internet Connection", 1000);
         return false;
    }
    else
    {
        if(info.isConnected())
        {
            Log.d(TAG," internet connection available...");
            return true;
        }
        else
        {
            Log.d(TAG," internet connection");
            return true;
        }

    }
}
 }

In your Activity Class

        if(CheckNetwork.isInternetAvailable(YourActivity.this))
             {
               //your studd
             }

Use f7 to disconnect and connect in emulator. I am not sure. Google it. Alternatively you can use broadcast receiver or service.

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

Try this:

public static boolean isNetworkAvailable(Context context)
{
    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getActiveNetworkInfo();

    return (info != null);
}
Artyom Kiriliyk
  • 2,513
  • 1
  • 17
  • 21