如何检测wifi连接到设备

时间:2011-12-16 11:27:55

标签: android android-wifi

请参阅代码

    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

    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);
    }


}//

}

当只有设备与wifi连接时,我想按下按钮,但如果设备也与edge / gprs连接,则使用上面的代码条件成为真 请问我该怎么做

4 个答案:

答案 0 :(得分:1)

我也使用此代码检查Wifi和移动网络连接,

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;
  }

答案 1 :(得分:0)

我正在使用以下功能检查特定时刻网络是否可用(检查连接类型)。

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;
 }

答案 2 :(得分:0)

我认为这会有所帮助:

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;    
        }
    }
}

答案 3 :(得分:0)

迟到但可能对其他人有所帮助。我也在寻找一种方法来看看wifi状态的变化。有一个广播意图操作,您可以侦听名为NETWORK_STATE_CHANGED_ACTION而不是CONNECTIVITY_ACTION。这个只处理wifi状态的变化。

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