Wifi的五种状态
WifiManager.WIFI_STATE_ENABLED//已打开 WifiManager.WIFI_STATE_DISABLING//正在关闭 WifiManager.WIFI_STATE_ENABLING//正在打开 WifiManager.WIFI_STATE_DISABLED//已关闭 WifiManager.WIFI_STATE_UNKNOWN//未知
一、打开关闭Wifi
添加权限
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> <uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
private ToggleButton toggleButton1; private WifiManager mWiFiManager01; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.wifi); toggleButton1 = (ToggleButton)findViewById(R.id.toggleButton1); /* 以getSystemService取得WIFI_SERVICE */ mWiFiManager01 = (WifiManager) this.getSystemService(Context.WIFI_SERVICE); if(mWiFiManager01.getWifiState() == WifiManager.WIFI_STATE_ENABLED){ toggleButton1.setChecked(true); toggleButton1.setEnabled(true); } else if(mWiFiManager01.getWifiState() == WifiManager.WIFI_STATE_DISABLED){ toggleButton1.setChecked(false); toggleButton1.setEnabled(true); } else{ toggleButton1.setChecked(false); toggleButton1.setEnabled(false); } toggleButton1.setOnCheckedChangeListener(new ToggleButton.OnCheckedChangeListener(){ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked == true){ mWiFiManager01.setWifiEnabled(true); } else{ mWiFiManager01.setWifiEnabled(false); } } }); }
二、搜索wifi
private Button button1; private WifiManager mWiFiManager01; private SimpleAdapter listItemAdapter; // ListView的适配器 private ArrayList<HashMap<String, Object>> listItem; // ListView的数据源,这里是一个HashMap的列表 private ListView myList; // ListView控件 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.wifi_connect); button1 = (Button) findViewById(R.id.button1); mWiFiManager01 = (WifiManager) this .getSystemService(Context.WIFI_SERVICE); mWiFiManager01.startScan(); listItem = new ArrayList<HashMap<String, Object>>(); listItemAdapter = new SimpleAdapter(this, listItem, R.layout.list_item, new String[] { "title", "text" }, new int[] { R.id.ItemTitle, R.id.ItemText }); myList = (ListView) findViewById(R.id.list); myList.setAdapter(listItemAdapter); button1.setOnClickListener(new OnClickListener() { public void onClick(View v) { List<WifiConfiguration> wifiConf = mWiFiManager01 .getConfiguredNetworks(); if ((!mWiFiManager01.isWifiEnabled()) && (mWiFiManager01.getWifiState() != WifiManager.WIFI_STATE_ENABLING)) { mWiFiManager01.setWifiEnabled(true); } if (wifiConf.size() > 0) { int netWorkId = wifiConf.get(0).networkId; mWiFiManager01.enableNetwork(netWorkId, true); int index = 0; for (WifiConfiguration singleWifiConf : wifiConf) { addItem((index + 1) + " SSID." + singleWifiConf.SSID, "NetWorkID:" + singleWifiConf.networkId); index = index + 1; } } } }); } private void addItem(String title, String str) { HashMap<String, Object> map = new HashMap<String, Object>(); map.put("title", title); map.put("text", str); listItem.add(map); listItemAdapter.notifyDataSetChanged(); }