Android:难以理解用于扫描 WiFi AP 并在列表视图中列出它们的 wifi 接收器和适配器概念

Android: trouble in understanding wifi receiver and adapter concepts used to scan WiFi APs and list them in listview

public class MainActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener{

// Declare View variables
private Button mRefreshButton;
private Switch mWifiSwitch;
private ListView mAPListView;
private List<ScanResult> mWifiList;
private List<String> mListOfProviders;
private ListAdapter mAdapter;
private WifiManager mWifiManager;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mListOfProviders = new ArrayList<String>();
    mAPListView = (ListView) findViewById(R.id.APListView);
    mRefreshButton = (Button) findViewById(R.id.refreshButton);
    mWifiSwitch = (Switch) findViewById(R.id.WiFiSwitch);
    mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);

    boolean wasEnabled = mWifiManager.isWifiEnabled();
    if (wasEnabled){
        mWifiSwitch.setChecked(true);
    }
    mWifiSwitch.setOnCheckedChangeListener(this);

    //wifiReciever = new WifiScanReceiver();
    mWifiManager.startScan();
    mWifiList = mWifiManager.getScanResults();

}


@Override
public void onClick(View view)
{

}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (isChecked){
        mWifiManager.setWifiEnabled(true);
    }
    else {
        mWifiManager.setWifiEnabled(false);
    }
}

}

我对 Android 开发非常陌生,只是构建一个 android 应用程序来扫描可用的 AP 并列出它们。

但是,我无法理解最常用于构建这些功能的适配器和 wifi 接收器的概念和用法。

你能帮我看看我应该怎么做才能在开始扫描后实际存储和显示 AP 信息吗?

谢谢。

您已经创建了 ListView 并生成了可用连接列表,因此大部分工作已完成。剩下要做的是用列表中的对象填充 ListView,为此您需要使用适配器。

但首先您应该将连接的名称添加到您创建的 List<String> 中:

for ( ScanResult result :  mWifiList ) { 
 mListOfProviders.add( result.SSID); //This will add the SSID of the connection
}

然后您将把这个列表传递给 ArrayAdapter

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, android.R.id.text1, mListOfProviders);
mAPListView.setAdapter(adapter);

祝你好运:)