我正在努力做一件简单的事情而没有任何成功。我想获得WiFi扫描结果。
我阅读并试用这些链接没有任何好结果:
How can I get Android Wifi Scan Results into a list?
这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initWifiSpinner();
initWifi();
}
@Override
public void onResume() {
super.onResume();
getWifiPermissions();
}
@Override
protected void onPostResume() {
super.onPostResume();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case LOCAL_RC_WIFI_PERMISIONS:
handleWifiPermissions();
break;
}
}
/***************************************************************************************
*
* WIFI Handler
*
****************************************************************************************/
private void initWifi() {
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiScanReceiver = new WifiScanReceiver();
enableWifi();
getWifiPermissions();
}
private void enableWifi() {
if (false == wifiManager.isWifiEnabled()) {
Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled", Toast.LENGTH_LONG).show();
wifiManager.setWifiEnabled(true);
}
}
private void initWifiSpinner() {
Spinner wifiSpinner = (Spinner) findViewById(R.id.spinnerWifi);
wifiSpinner.setOnItemSelectedListener(new WifiSpinnerActivity());
wifiSpinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinnerListArray);
wifiSpinner.setAdapter(wifiSpinnerAdapter);
}
private void getWifiPermissions() {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, LOCAL_RC_WIFI_PERMISIONS );
} else {
handleWifiPermissions();
}
}
private void handleWifiPermissions() {
registerReceiver(wifiScanReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
boolean startScan = wifiManager.startScan();
if (startScan) {
List<ScanResult> results = wifiManager.getScanResults();
Toast.makeText(getApplicationContext(), "SSID list size: " + results.size(), Toast.LENGTH_SHORT).show();
}
}
public class WifiScanReceiver extends BroadcastReceiver {
int local = 0;
@Override
public void onReceive(Context context, Intent intent) {
List<ScanResult> results = wifiManager.getScanResults();
int size = results.size();
Toast.makeText(getApplicationContext(), ++local + " Result list size: " + size, Toast.LENGTH_SHORT).show();
unregisterReceiver(this);
}
}
我拥有以下权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
我有两个问题:
List<ScanResult> results = wifiManager.getScanResults();
的results.size()始终为0.
由于已知原因handleWifiPermissions
一次又一次地调用。
答案 0 :(得分:0)
试试这个,它对我有用:
select col1,col2,col3, CASE col4 WHEN true THEN "type1" END from ... where ...;
select col1,col2,col3, CASE col5 WHEN true THEN "type2" END from ... where ...;
select col1,col2,col3, CASE col6 WHEN true THEN "type3" END from ... where ...;
select col1,col2,col3, CASE col7 WHEN true THEN "type4" END from ... where ...;
答案 1 :(得分:0)
尝试在“设置”中启用“位置”。
我在Oreo(API27)设备上遇到了同样的问题。