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

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

使用Connectivity Manager Class,我们可以访问wifi或Internet网络:

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

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

如果我的Android设备连接到两者,我们可以在任何网络之间切换,还是可以禁用任何网络?喜欢使用功能:

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


HUWWW
浏览 2155回答 3
3回答

潇潇雨雨

我知道启用或禁用wifi:WifiManager&nbsp;wifiManager&nbsp;=&nbsp;(WifiManager)this.context.getSystemService(Context.WIFI_SERVICE);wifiManager.setWifiEnabled(status);状态可能是true或false根据要求。编辑:您还需要清单文件中的以下权限:&nbsp;<uses-permission&nbsp;android:name="android.permission.ACCESS_WIFI_STATE"/> &nbsp;<uses-permission&nbsp;android:name="android.permission.CHANGE_WIFI_STATE"/>

人到中年有点甜

启用WiFi:WifiManager&nbsp;wifi&nbsp;=&nbsp;(WifiManager)&nbsp;getSystemService(Context.WIFI_SERVICE);wifi.setWifiEnabled(true);要禁用WiFi:WifiManager&nbsp;wifi&nbsp;=&nbsp;(WifiManager)&nbsp;getSystemService(Context.WIFI_SERVICE);wifi.setWifiEnabled(false);注意:要使用WiFi状态进行访问,我们必须在AndroidManifest.xml文件中添加以下权限:android.permission.ACCESS_WIFI_STATE android.permission.UPDATE_DEVICE_STATS&nbsp; android.permission.CHANGE_WIFI_STATE

精慕HU

完整的解决方案:try&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;WifiManager&nbsp;wifi&nbsp;=&nbsp;(WifiManager)&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;context.getSystemService(Context.WIFI_SERVICE); &nbsp;&nbsp;&nbsp;&nbsp;WifiConfiguration&nbsp;wc&nbsp;=&nbsp;new&nbsp;WifiConfiguration(); &nbsp;&nbsp;&nbsp;&nbsp;wc.SSID&nbsp;=&nbsp;"\"SSIDName\""; &nbsp;&nbsp;&nbsp;&nbsp;wc.preSharedKey&nbsp;&nbsp;=&nbsp;"\"password\""; &nbsp;&nbsp;&nbsp;&nbsp;wc.hiddenSSID&nbsp;=&nbsp;true; &nbsp;&nbsp;&nbsp;&nbsp;wc.status&nbsp;=&nbsp;WifiConfiguration.Status.ENABLED;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); &nbsp;&nbsp;&nbsp;&nbsp;wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); &nbsp;&nbsp;&nbsp;&nbsp;wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); &nbsp;&nbsp;&nbsp;&nbsp;wc.allowedPairwiseCiphers&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.set(WifiConfiguration.PairwiseCipher.TKIP); &nbsp;&nbsp;&nbsp;&nbsp;wc.allowedPairwiseCiphers&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.set(WifiConfiguration.PairwiseCipher.CCMP); &nbsp;&nbsp;&nbsp;&nbsp;wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN); &nbsp;&nbsp;&nbsp;&nbsp;boolean&nbsp;b=wifi.isWifiEnabled(); &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(b)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wifi.setWifiEnabled(false); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Toast.makeText(context,&nbsp;"yes",&nbsp;Toast.LENGTH_SHORT).show(); &nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wifi.setWifiEnabled(true); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Toast.makeText(context,&nbsp;"no",&nbsp;Toast.LENGTH_SHORT).show(); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;//Log.d("WifiPreference",&nbsp;"enableNetwork&nbsp;returned&nbsp;"&nbsp;+&nbsp;b&nbsp;);}&nbsp;catch&nbsp;(Exception&nbsp;e)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();}参考:http://amitkumar-android.blogspot.com/p/installation-steps.html
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android