【发布时间】:2018-12-19 06:14:23
【问题描述】:
我想列出所有 wifi 网络并在列表中显示 ssid。我认为我应该使用广播来执行此操作,但我使用了几个代码但不起作用。我只需要列出所有 sside 并将它们显示给用户但是如果你告诉我如何显示 wifi 信号也很强大,它会很棒。 我使用此代码但不起作用:
public class WiFiDemo extends AppCompatActivity implements View.OnClickListener {
WifiManager wifi;
ListView lv;
TextView textStatus;
Button buttonScan;
int size = 0;
List<ScanResult> results;
String ITEM_KEY = "key";
ArrayList<HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>();
SimpleAdapter adapter;
/* Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wifidemo);
textStatus = (TextView) findViewById(R.id.textStatus);
buttonScan = (Button) findViewById(R.id.buttonScan);
buttonScan.setOnClickListener(this);
lv = (ListView) findViewById(R.id.list);
wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (wifi.isWifiEnabled() == false) {
Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled", Toast.LENGTH_LONG).show();
wifi.setWifiEnabled(true);
}
this.adapter = new SimpleAdapter(WiFiDemo.this, arraylist, R.layout.row, new String[]{ITEM_KEY}, new int[]{R.id.list_value});
lv.setAdapter(this.adapter);
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent intent) {
results = wifi.getScanResults();
size = results.size();
}
}, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}
public void onClick(View view) {
arraylist.clear();
wifi.startScan();
Toast.makeText(this, "Scanning...." + size, Toast.LENGTH_SHORT).show();
try {
size = size - 1;
while (size >= 0) {
HashMap<String, String> item = new HashMap<String, String>();
item.put(ITEM_KEY, results.get(size).SSID + " " + results.get(size).capabilities);
arraylist.add(item);
size--;
adapter.notifyDataSetChanged();
}
} catch (Exception e) {
}
}
}
【问题讨论】:
-
在帖子中看到这个答案我认为它可以帮助你*.com/a/7527380/2818627
-
这段代码一模一样
-
为了获得wifi信号强度,你可以使用这个答案*.com/a/30901663/2818627enter link description here
标签: android broadcastreceiver android-wifi ssid