我試圖讓WiFi網絡可用這樣做:掃描在Android的WiFi網絡與位置關閉
private WifiManager wifiManager;
private List<ScanResult> scanResults;
class WifiReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive");
scanResults = wifiManager.getScanResults();
Log.d(TAG, "" + scanResults.size());
for (ScanResult scanResult : scanResults) {
Log.d(TAG, scanResult.SSID);
}
}
}
private class TaskScan extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
wifiManager.setWifiEnabled(true);
wifiManager.startScan();
return null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wifiManager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiReceiver wifiReceiver = new WifiReceiver();
registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
try {
(new TaskScan()).execute().get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
不過這個名單scanResults的大小始終是0時的位置被關閉。
是否有無線掃描wifi網絡沒有啓用位置?