0

Please see the code

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);
     mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
     receiverWifi = new WifiReceiver();
     mIntentFilter = new IntentFilter();
     final IntentFilter mIFNetwork = new IntentFilter();
        mIFNetwork.addAction(android.net.ConnectivityManager.CONNECTIVITY_ACTION); //"android.net.conn.CONNECTIVITY_CHANGE"
        registerReceiver(receiverWifi, mIFNetwork);

     //   mainWifi.startScan();


    haveNetworkConnection() ;
}// end of the function

and

    class WifiReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
//  Toast.makeText(SecondActivity.this, "Message is show", Toast.LENGTH_LONG).show();
//  haveNetworkConnection() ;

    boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);

    if (noConnectivity) {
         ImageButton b=(ImageButton)findViewById(R.id.button4);
         b.setBackgroundResource(R.drawable.lightedbutton_off);
    } else {
         ImageButton b=(ImageButton)findViewById(R.id.button4);
         b.setBackgroundResource(R.drawable.lightedbutton_on);
    }


}//

}

i want to on the button when only device is connected with wifi , but using above code condition becomes true if device is also connected with edge/gprs Please how can I do this

Ali
  • 10,774
  • 10
  • 56
  • 83
  • possible duplicate of [how to see if wifi is connected in android](http://stackoverflow.com/questions/3841317/how-to-see-if-wifi-is-connected-in-android) – JB. Dec 16 '11 at 12:00
  • see http://stackoverflow.com/a/3841407/329034 – mbwasi Dec 16 '11 at 12:02

4 Answers4

1

I use this code to check Wifi and mobile network connection also,

public 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();
        System.out.println("wifiNetwork.isAvailable(): "+wifiNetwork.isAvailable());
        System.out.println("wifiNetwork.isConnected(): "+wifiNetwork.isConnected());
        System.out.println("wifiNetwork.isFaileOver(): "+wifiNetwork.isFailover());
        System.out.println("wifiNetwork.isConnectedOrConnecting: "+state);
              }

        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;
  }
Karthi
  • 13,624
  • 10
  • 53
  • 76
0

Late but might be of help to others. I too was looking for a way to see wifi state changes. There is a broadcast intent action that you can listen for named NETWORK_STATE_CHANGED_ACTION instead of CONNECTIVITY_ACTION. This one only deals with changes in wifi state.

http://developer.android.com/reference/android/net/wifi/WifiManager.html#NETWORK_STATE_CHANGED_ACTION

Moemars
  • 4,692
  • 3
  • 27
  • 30
0

I'm using the function below to check if network is available (with checking the type of connection) in a particular moment.

public boolean isNetworkAvailable() {
    final ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo wifiNetInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    final NetworkInfo mobileNetInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    if (wifiNetInfo != null && wifiNetInfo.isAvailable() && wifiNetInfo.isConnected()) {
                    // we are connected via WiFi
        return true;
    } else if (mobileNetInfo != null && mobileNetInfo.isAvailable() && mobileNetInfo.isConnected()) {
                    // we are connected via mobile data (GPRS, 3G, etc.)
        return true;
    }

    return false;
 }
GrAnd
  • 10,141
  • 3
  • 31
  • 43
0

I think this will help:

ConnectivityManager conMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInf = conMgr.getAllNetworkInfo();

boolean noConnectivity = false;

for(NetworkInfo inf : netInf){
    if(inf.getTypeName().contains("WIFI")) {
        if(inf.isConnected()){
           noConnectivity = true;
        }  else{
            //if you wanna do smth else;    
        }
    }
}
hovanessyan
  • 30,580
  • 6
  • 55
  • 83