-1

I am trying to check internet state normally. When I enable internet and then I run this app it shows "Internet is connected". When I stop internet and then again run this app it again it will show "not connected to internet". But when I run this app in an emulator it always shows Internet connected. Please tell me what's the problem.

//check Internet connection.
private boolean checkInternetConnection() {
    ConnectivityManager check = (ConnectivityManager) this.context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (check != null) {
        NetworkInfo[] info = check.getAllNetworkInfo();
        if (info != null)  
            for (int i = 0; i <info.length; i++) 
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    Toast.makeText(context, "Internet is connected", Toast.LENGTH_SHORT).show();
                }
             return true;
    } else {
        Toast.makeText(context, "not connected to internet", Toast.LENGTH_SHORT).show();
        return false;
    }

}
SilentKiller
  • 6,944
  • 6
  • 40
  • 75

5 Answers5

0

I guess the problem is in line

NetworkInfo[] info = check.getAllNetworkInfo();

you will only get active network info you can try the following method i tested it works like a charm:

public boolean isNetworkUp() {
    ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork != null && activeNetwork.isConnectedOrConnecting()) {
        if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
            return true;
        }
    }
    return false;
}
SilentKiller
  • 6,944
  • 6
  • 40
  • 75
0

you can try this method.......

private boolean isNetConnected() {

ConnectivityManager cm =  (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

                NetworkInfo ni = cm.getActiveNetworkInfo();

                if(ni != null && ni.isConnectedOrConnecting()){
                    return true;
                }
                return false;

}

0

Hi Use the Following Code..

/**
 * Checks if is connection.
 * 
 * @param con
 *            the con
 * @return true, if is connection
 */
public static boolean isConnection(Context con) {
    ConnectivityManager conMgr = (ConnectivityManager) con
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (conMgr.getActiveNetworkInfo() != null
            && conMgr.getActiveNetworkInfo().isAvailable()
            && conMgr.getActiveNetworkInfo().isConnected()) {
        return true;
    } else {
        return false;
    }
}

Let me know your feedbacks.

Sivakumar
  • 633
  • 4
  • 9
0

You can also use F8 to set the cell network on/off. Check this link: http://developer.android.com/tools/help/emulator.html

cs95
  • 379,657
  • 97
  • 704
  • 746
Niks
  • 500
  • 4
  • 7
0

It would be a solution for your question.

public boolean isNetworkAvailable() {
            ConnectivityManager connectivityManager 
                  = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
            if (activeNetworkInfo != null && activeNetworkInfo.isConnected()){ 
            return true;
            }else {
                return false;
            }
        }
Zafer Celaloglu
  • 1,438
  • 1
  • 17
  • 28