Android:如何以编程方式启用/禁用Wifi或Internet连接

使用Connectivity Manager Class,我们可以访问wifi或Internetnetworking:

ConnectivityManager connec = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); // ARE WE CONNECTED TO THE NET if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED || connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED ) { // ... } 

其中01分别指移动和wifi连接

如果我的Android设备连接到两者,我们可以在任何networking之间切换,或者我们可以禁用任何networking? 像使用一个函数一样:

 connec.getNetworkInfo(0).setState(NetworkInfo.State.DISCONNECTED); 

我知道启用或禁用wifi:

 WifiManager wifiManager = (WifiManager)this.context.getSystemService(Context.WIFI_SERVICE); wifiManager.setWifiEnabled(status); 

根据要求,状态可能为truefalse

启用WiFi:

 WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); wifi.setWifiEnabled(true); 

禁用WiFi:

 WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); wifi.setWifiEnabled(false); 

注意:要使用WiFi状态访问,我们必须在AndroidManifest.xml文件中添加以下权限:

 android.permission.ACCESS_WIFI_STATE android.permission.UPDATE_DEVICE_STATS android.permission.CHANGE_WIFI_STATE 

完成解决scheme..

  try { WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiConfiguration wc = new WifiConfiguration(); wc.SSID = "\"SSIDName\""; wc.preSharedKey = "\"password\""; wc.hiddenSSID = true; wc.status = WifiConfiguration.Status.ENABLED; wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN); boolean b=wifi.isWifiEnabled(); if(b){ wifi.setWifiEnabled(false); Toast.makeText(context,"yes", Toast.LENGTH_SHORT).show(); }else { wifi.setWifiEnabled(true); Toast.makeText(context, "no", Toast.LENGTH_SHORT).show(); } //Log.d("WifiPreference", "enableNetwork returned " + b ); } catch (Exception e) { e.printStackTrace(); } 

…………………………………….. http:// amitkumar- android.blogspot.com/p/installation-steps.html

要启用禁用Wifi使用WifiManager类来获取系统(安卓设备)的无线服务:

 WifiManager wifi =(WifiManager)getSystemService(Context.WIFI_SERVICE); 

现在使用WifiManager类的对象wifi来获得wifi状态:

 if(wifi.isWifiEnabled()) //Perform Operation else //Other Operation 

最重要的是别忘了在Android清单文件中给出以下权限:

 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> <uses-permission android:name="android.permission.UPDATE_DEVICE_STATS" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> 

要获得详细的信息和项目的完整示例代码,以启用/禁用Android上的Wifi访问我的网站链接 。

在清单中添加此权限,并使用上面的代码更改WiFi状态:

  <!--permission ge enable and disable WIFI in android--> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>