在我的应用程序中,我正在使用WifiManger扫描附近的网络。另外,我正在使用广播接收器来分析扫描结果。我正在寻找特定的网络,所以如果30秒后我没有找到我要寻找的任何网络,我想停止扫描。我该如何设置时间限制?
这是我的广播接收器的代码:
class IoTDevicesReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
wifiList = wifi.getScanResults();
netCount = wifiList.size();
Log.e("Wifi", "Total wifi networks " + netCount);
new Thread(new Runnable() {
@Override
public void run() {
try{
networks.clear();
netCount -= 1;
while(netCount >= 0){
if(wifiList.get(netCount).SSID.contains("Type I") ||
wifiList.get(netCount).SSID.contains("Type II")) {
boolean isSecured = (wifiList.get(netCount).capabilities.contains("WEP") || wifiList.get(netCount).capabilities.contains("WPA"));
String encryptionType;
if(wifiList.get(netCount).capabilities.contains("WEP"))
encryptionType = "WEP";
else if(wifiList.get(netCount).capabilities.contains("WPA"))
encryptionType = "WPA";
else
encryptionType = "NONE";
Network network = new Network(wifiList.get(netCount).SSID.toString(),
String.valueOf(wifiList.get(netCount).level), encryptionType, !isSecured);
Log.d("WiFi", network.getNetworkName());
networks.add(network);
runOnUiThread(new Runnable() {
@Override
public void run() {
mAdapter.notifyDataSetChanged();
mProgressbar.setVisibility(View.GONE);
mWaitMessage.setVisibility(View.GONE);
}
});
}
netCount -= 1;
}
}
catch (Exception e){
Log.d("Wifi", e.getMessage());
}
}
}).start();
}
}
我使用这种方法开始扫描:
private void startWifiScanning()
{
wifi.startScan();
}