用于WIFI检测的Android服务

时间:2014-08-04 10:44:45

标签: android

我正在开发一个应用程序,它应该列出所有的wifi热点,当wifi热点更改时,列表应该刷新并显示wifi热点的新列表。搜索过,但不知道该怎么做。

1 个答案:

答案 0 :(得分:0)

使用此:

WifiManager wifi;
WifiReciever wifiReciever;
int size = 0;
List<ScanResult> results;
String[] ssidList;
String[] bssidList;
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    if (wifi.isWifiEnabled() == false) {
                    Toast.makeText(getApplicationContext(),
                            getString("wifi is not enabled"),
                            Toast.LENGTH_LONG).show();
                    return;
                }
                wifiReciever = new WifiReciever();
                IntentFilter intentFilter = new IntentFilter(
                        WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
                registerReceiver(wifiReciever, intentFilter);
                wifi.startScan();

接收者类:

        class WifiReciever extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {

            results = wifi.getScanResults();
            ssidList = new String[results.size()];
            bssidList = new String[results.size()];
            if (results != null && results.size() > 0) {
                unregisterReceiver(wifiReciever);
                for (int i = 0; i < results.size(); i++) {
                    Log.i("TAG", results.get(i).SSID + "     "
                            + results.get(i).BSSID);
                    ssidList[i] = results.get(i).SSID.toString();
                    bssidList[i] = results.get(i).BSSID.toString();

                }
                AlertDialog.Builder builder = new Builder(SettingsActivity.this);
                builder.setTitle(getString(R.string.title_choose_wifi));
                builder.setItems(ssidList,
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                            // do your logic
                            }
                        });
                builder.show();

            } else {
                AndyUtills.showToast(getString("No wifi available"),
                        SettingsActivity.this);
            }

        }
    }
}