我有一个关于在我的应用程序中使用WiFiManger的问题。 代码很简单,我创建了这项服务,扫描网络以获得可用的WiFi:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mapName = intent.getExtras().getString("mapName");
sendNotification();
mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
// Check for wifi is disabled
if (!mainWifi.isWifiEnabled()) {
// If wifi disabled then enable it
Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled",
Toast.LENGTH_LONG).show();
mainWifi.setWifiEnabled(true);
}
mainWifi.startScan();
List<ScanResult> scanResults=mainWifi.getScanResults();
readAps=getAps(scanResults);
//List of AP received in current position
String result=compareRP();
if(!result.isEmpty())
Toast.makeText(this, "Localized in RP: "+result, Toast.LENGTH_LONG).show();
else
Toast.makeText(this, "Unable to localize you in this map", Toast.LENGTH_LONG).show();
return START_NOT_STICKY;
}
private ArrayList<AccessPoint> getAps(List<ScanResult> scanResults) {
ArrayList<AccessPoint> temp = new ArrayList<AccessPoint>();
for(ScanResult s:scanResults){
temp.add(new AccessPoint(s.SSID,s.level,s.frequency));
}
return temp;
}
我如何添加这个条件:我想等待一个“新鲜”的WiFi列表,来自:
mainwifi.startScan();
方法返回。
现在我的服务仍然继续使用群集匹配算法,我不想要它。我该怎么办?
感谢您提供任何帮助!
佛瑞德
答案 0 :(得分:0)
您必须在活动中使用BroadcastReceiver
。
private final BroadcastReceiver mWifiScanReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent intent) {
// This condition is not necessary if you listen to only one action
if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
List<ScanResult> mScanResults = mWifiManager.getScanResults();
Toast.makeText(getApplicationContext(), "Scan results are available", Toast.LENGTH_LONG).show();
// Do what you want
}
}
};
private WifiManager mWifiManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
// You can add multiple actions...
registerReceiver(mWifiScanReceiver, intentFilter);
// ...
mWifiManager.startScan();
}