的Android 4.0 - 检查连接到WiFi

问题描述:

我用下面的函数来检查连接到WiFi热点点:的Android 4.0 - 检查连接到WiFi

public boolean IsWiFiConnected(){ 
List<WifiConfiguration> wifiConfigList = wifiManager.getConfiguredNetworks(); 
boolean retVal=false; 
    for(WifiConfiguration wifiConf : wifiConfigList){    
     if(wifiConf.status==WifiConfiguration.Status.CURRENT){ 
      retVal=true; 
      break; 
     } 
    } 
return retVal; 
} 

在Android 4.0的它总是返回false。它在以前的版本上运行良好。 谢谢

+0

...因为它返回启用。我用4.2.2试过,发现它再次返回CURRENT。不知道为什么...看[这里](http://stackoverflow.com/questions/22795829/differences-between-wificonfiguration-status)。 – 2014-04-01 20:13:35

有一个比你更快和更可靠的方法。

public boolean IsWiFiConnected() { 
    ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); 
    NetworkInfo netInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 

    return netInfo.isConnected(); 
} 

只是对加布里埃尔的答案扩大,因为它可能是值得检查用户的任何数据连接,即; WiFi或数据。这也会显示一个对话框询问用户他们是否想用Intent打开WiFi。希望这可以帮助你!

private boolean haveNetworkConnection() { 
    boolean haveConnectedWifi = false; 
    boolean haveConnectedMobile = false; 

    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo[] netInfo = cm.getAllNetworkInfo(); 
    for (NetworkInfo ni : netInfo) { 
     if (ni.getTypeName().equalsIgnoreCase("WIFI")) 
      if (ni.isConnected()) 
       haveConnectedWifi = true; 
     if (ni.getTypeName().equalsIgnoreCase("MOBILE")) 
      if (ni.isConnected()) 
       haveConnectedMobile = true; 

    } 
    if (haveConnectedWifi == false && haveConnectedMobile == false) { 
     Log.d("Network State", "false"); 
     AlertDialog.Builder alert = new AlertDialog.Builder(YourActivity.this);     
     alert.setTitle("No Data Connection!"); 
     alert.setMessage("You have no data connection."); 
      alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 

       public void onClick(DialogInterface dialog, int whichButton) { 
        // TODO Auto-generated method stub 
        final Intent intent = new Intent(Intent.ACTION_MAIN, null); 
        intent.addCategory(Intent.CATEGORY_LAUNCHER); 
        final ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.wifi.WifiSettings"); 
        intent.setComponent(cn); 
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        startActivity(intent); 
       } 

      }); 
      alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 

       public void onClick(DialogInterface dialog, int which) { 
        // TODO Auto-generated method stub 
        return; 
       } 
      }); 
      alert.show(); 

    }else{ 
     //do something else 
    } 
    return haveConnectedWifi || haveConnectedMobile; 
} 

检查无线网络连接状态:

ConnectivityManager conMgr; 
NetworkInfo netInfo; 
WifiManager wifiMgr; 

conMgr=(ConnectivityManager)getSystemService(context.WIFI_Service); 
netInfo=conMgr.getActiveNetworkInfo(); 
if(!(netInfo==null)) 
{ 
if(WifiMgr.isWifiEnabled()) 
{ 
//wifi enabled 
} 
else 
{ 
//wifi disabled i.e not available 
} 
}