安卓网络连接情况全解:包括网络连接状态的监听、网络数据使用状态的监听、获取当前网络连接情况、启动wifi、获取当前连接wifi的网络情况、扫描wifi热点

移动开发 waitig 616℃ 百度已收录 0评论

全栈工程师开发手册 (作者:栾鹏)

安卓教程全解

安卓网络连接情况全解:包括网络连接状态的监听、网络数据使用状态的监听、获取当前网络连接情况、启动wifi、获取当前连接wifi的网络情况、扫描wifi热点

网络连接情况的监听和网络数据使用情况的监听

  private void listen_net() 
  {
        //注册用于监听后台数据使用情况变化的BroadcastReceiver
        registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Log.v("网络数据使用变化", "");
            }
          },new IntentFilter(ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED)   //触发监听的广播行为
        );

        //用于监听网络连接变化的BroadcastReceiver
        registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Log.v("网络连接变化", "网络状态已经改变"); 
                netinfo();
            }
          },new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION) //触发监听的广播行为
        ); 
  }

获取当前网络信息

移动网络和wifi网络都是网络情况的一种情况

  public void netinfo() 
  {
        //访问Connectivity Manager网络连接服务
        String service = Context.CONNECTIVITY_SERVICE;
        final ConnectivityManager connectivity = (ConnectivityManager)getSystemService(service);

        //访问网络信息
        NetworkInfo activeNetwork = connectivity.getActiveNetworkInfo();
        boolean isConnected = ((activeNetwork  != null) && (activeNetwork.isConnectedOrConnecting()));
        boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
        Log.v("网络信息", "是否已连接网络:"+isConnected);
        Log.v("网络信息", "是否连接了wifi:"+isWiFi);
        if(isConnected)
        {
            String netname=activeNetwork.getTypeName();
            Log.v("网络信息", "网络名称:"+netname);
        }
  }

启动wifi、扫描wifi热点

wifi是网络连接的一种

  private void scan_wifi() {
        //访问Wi-Fi Manager
        String service = Context.WIFI_SERVICE;
        final WifiManager wifi = (WifiManager)getSystemService(service);

        //监听并更改Wi-Fi状态
        if (!wifi.isWifiEnabled())
          if (wifi.getWifiState() != WifiManager.WIFI_STATE_ENABLING)
            wifi.setWifiEnabled(true);

      //扫描wifi接入点,在开启了wifi,但还没有连上任何一个ssid时才会触发
        //注册用于接收扫描结果的broadcast receiver
        registerReceiver(new BroadcastReceiver() {
          @Override
          public void onReceive(Context context, Intent intent) {
            List<ScanResult> results = wifi.getScanResults();
            ScanResult bestSignal = null;
            for (ScanResult result : results) {
              if (bestSignal == null || WifiManager.compareSignalLevel(bestSignal.level,result.level) < 0)
                bestSignal = result;
            }
            String connSummary = String.format("%s 个网络被发现,其中 %s 是信号最强的",results.size(),bestSignal.SSID);
            Log.d("wifi扫描", connSummary);
          }
        }, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));

        //开始扫描
        wifi.startScan();
  }

获取当前wifi连接情况

private void wifiinfo() 
  {
        //访问Wi-Fi Manager
        String service = Context.WIFI_SERVICE;
        final WifiManager wifi = (WifiManager)getSystemService(service);


        //监听并更改Wi-Fi状态
        if (!wifi.isWifiEnabled())
          if (wifi.getWifiState() != WifiManager.WIFI_STATE_ENABLING)
            wifi.setWifiEnabled(true);

        //查询活动的网络连接,在连接到某个ssid时才触发
        WifiInfo info = wifi.getConnectionInfo();
        if (info.getBSSID() != null) {
          int strength = WifiManager.calculateSignalLevel(info.getRssi(), 5); //信号强弱
          int speed = info.getLinkSpeed();//网上速率
          String units = WifiInfo.LINK_SPEED_UNITS; //速率单位
          String ssid = info.getSSID(); //ssid
          String mac = info.getMacAddress(); //mac地址
          int ip = info.getIpAddress();  //ip地址

          String cSummary = String.format("当前wifi正连接到%s,速度%s%s,信号强度 %s/5",ssid, speed, units, strength);
          Log.d("wifi连接", cSummary);
        }


  }

点赞 (0)分享 (0)