其他分享
首页 > 其他分享> > 如何获得可用的wifi网络并将其显示在android中的列表中

如何获得可用的wifi网络并将其显示在android中的列表中

作者:互联网

朋友们,我想找到所有可用的WiFi网络并将其显示在我尝试过的列表中,如下所示.但它不起作用.我已经编辑了我的代码,现在我得到了结果但是得到了我不需要的所有结果.我只需要列表中的wifi网络名称.

public class MainActivity extends Activity {

    TextView mainText;
    WifiManager mainWifi;
    WifiReceiver receiverWifi;
    List<ScanResult> wifiList;
    StringBuilder sb = new StringBuilder();

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mainText = (TextView) findViewById(R.id.tv1);
        mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

        if (mainWifi.isWifiEnabled() == false)
        {  
             // If wifi disabled then enable it
             Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled",
             Toast.LENGTH_LONG).show();
             mainWifi.setWifiEnabled(true);
         } 

         receiverWifi = new WifiReceiver();
         registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
         mainWifi.startScan();
         mainText.setText("Starting Scan...");

     }

     public boolean onCreateOptionsMenu(Menu menu) {
            menu.add(0, 0, 0, "Refresh");
            return super.onCreateOptionsMenu(menu);
     }

     public boolean onMenuItemSelected(int featureId, MenuItem item) {
            mainWifi.startScan();
            mainText.setText("Starting Scan");
            return super.onMenuItemSelected(featureId, item);
     }

     protected void onPause() {
            unregisterReceiver(receiverWifi);
            super.onPause();
     }

     protected void onResume() {
            registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
            super.onResume();
     }

        // Broadcast receiver class called its receive method
        // when number of wifi connections changed

      class WifiReceiver extends BroadcastReceiver {

            // This method call when number of wifi connections changed
            public void onReceive(Context c, Intent intent) {

                sb = new StringBuilder();
                wifiList = mainWifi.getScanResults();
                sb.append("\n        Number Of Wifi connections :"+wifiList.size()+"\n\n");

                for(int i = 0; i < wifiList.size(); i++){

                    sb.append(new Integer(i+1).toString() + ". ");
                    sb.append((wifiList.get(i)).toString());
                    sb.append("\n\n");
                }

                mainText.setText(sb); 
            }

       }
}

解决方法:

您需要创建一个BroadcastReceiver来监听Wifi扫描结果:

private final BroadcastReceiver mWifiScanReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context c, Intent intent) {
        if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
            List<ScanResult> mScanResults = mWifiManager.getScanResults();
            // add your logic here
        }
    }
}

在onCreate()中,您将分配mWifiManager并启动扫描:

mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
registerReceiver(mWifiScanReceiver,
        new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
mWifiManager.startScan();

只有拥有适当的权限,getScanResults()才会返回数据.为此,请将以下两行之一添加到AndroidManifest.xml:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

另请注意,在API 23中,必须在运行时请求权限. (对于实验室环境,您还可以在“设置”中手动授予权限,而不需要编码,但不建议用于最终用户应用程序.)

请注意,每次有新的扫描结果可用时,处理扫描结果的代码都会运行,从而更新结果.

标签:android,android-listview,android-wifi,wifimanager
来源: https://codeday.me/bug/20190917/1810005.html