黑马程序员技术交流社区

标题: Android wifi开关 [打印本页]

作者: 老衲玩IT    时间: 2013-8-25 15:49
标题: Android wifi开关
本帖最后由 老衲玩IT 于 2013-8-25 15:56 编辑
  1. //在layout.xml中为 toggleButton设置onclick属性为onWifiButton
  2. public void onWifiButton(View view) {//按下之后
  3.                 toggleButton=(ToggleButton) view;
  4.                 Intent intent=new Intent(Settings.ACTION_WIFI_SETTINGS);
  5.                 startActivity(intent);
  6.                 wifiManager.setWifiEnabled(toggleButton.isChecked());
  7.                 new Thread(new Runnable() {
  8.                         
  9.                         @Override
  10.                         public void run() {
  11.                                 long timeout=10000;//10秒钟
  12.                                 while (flag) {
  13.                                         if(wifiManager.isWifiEnabled()){
  14.                                                 StringBuilder sb=new StringBuilder();
  15.                                                 WifiInfo wifiInfo=wifiManager.getConnectionInfo();
  16.                                                 sb.append("MAC地址:"+wifiInfo.getMacAddress()+"\n");
  17.                                                 sb.append("接入点BSSID:"+wifiInfo.getBSSID()+"\n");
  18.                                                 sb.append("IP地址(int):"+wifiInfo.getIpAddress()+"\n");
  19.                                                 sb.append("IP地址(hex):"+Integer.toHexString(wifiInfo.getIpAddress())+"\n");
  20.                                                 sb.append("IP地址:"+ipIntToString(wifiInfo.getIpAddress())+"\n");
  21.                                                 sb.append("连接速度:"+wifiInfo.getLinkSpeed()+"Mbps\n");
  22.                                                 
  23.                                                 sb.append("\n已配置的无线网络\n");
  24.                                                 for (int i = 0; i < wifiManager.getConfiguredNetworks().size(); i++) {
  25.                                                         WifiConfiguration wifiConfiguration=wifiManager.getConfiguredNetworks().get(i);
  26.                                                         sb.append(wifiConfiguration.SSID+
  27.                                                                         ((wifiConfiguration.status==0)?"已连接":"未连接")+
  28.                                                                         "\n");
  29.                                                 }
  30.                                                 handler.sendMessage(handler.obtainMessage(1, sb));
  31.                                                 break;
  32.                                         }
  33.                                         try {
  34.                                                 Thread.sleep(1000);
  35.                                                 timeout-=1000;
  36.                                         } catch (InterruptedException e) {
  37.                                                 // TODO Auto-generated catch block
  38.                                                 e.printStackTrace();
  39.                                         }
  40.                                         if(timeout<=0){
  41.                                                 if (!wifiManager.isWifiEnabled()) {
  42. tvWifi.setText("");
  43. toggleButton.setCheced(false);
  44. }else{
  45. toggleButton.setCheced(true);
  46. }
  47.                                         }
  48.                                 }
  49.                         }
  50.                 }).start();

  51.         }

  52.         private String ipIntToString(int ip) {
  53.                 try {
  54.                         byte[] bytes=new byte[4];
  55.                         bytes[0]=(byte) (0xff & ip);
  56.                         bytes[0]=(byte) ((0xff00 & ip)>>8);
  57.                         bytes[0]=(byte) ((0xff0000 & ip)>>16);
  58.                         bytes[0]=(byte) ((0xff000000 & ip)>>24);
  59.                         return Inet4Address.getByAddress(bytes).getHostAddress();
  60.                 } catch (Exception e) {
  61.                         return "";// TODO: handle exception
  62.                 }
  63.                
  64.         }
复制代码

作者: 袁梦希    时间: 2013-8-25 15:58
学的挺好的




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2