在 Android 8.0 上,没有使用wifiManager获取wifi列表,在API级别26以下我得到了列表。
此函数返回WifiManager
对象
public static WifiManager getWifiManager(Context context) {
WifiManager wifiManager = null;
try {
wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
} catch (NullPointerException e) {
e.printStackTrace();
}
return wifiManager;
}
此函数返回wifi列表
public static List<ScanResult> getWifiScanResults(Boolean sorted, Context context) {
WifiManager wifiManager = NetworkUtil.getWifiManager(context);
List<ScanResult> wifiList = wifiManager.getScanResults();
//Remove results with empty ssid
List<ScanResult> wifiListNew = new ArrayList<>();
for (ScanResult scanResult : wifiList) {
if (!scanResult.SSID.equals(""))
wifiListNew.add(scanResult);
}
wifiList.clear();
wifiList.addAll(wifiListNew);
return wifiList;
}
我已注册BroadcastReceiver
WiFiMainActivity.this.registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
,这是startScan()方法
public static void startScan(Context context) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifiManager.startScan();
}
拥有以下权限的用户
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
无法跟踪android 8.0 Oreo上的错误,是否还有其他任何我遗漏的权限。
答案 0 :(得分:7)
自Android 8
引入background execution limitation BroadcastReceiver
之后,Manifest
中注册的大部分Context
行动将不再发送,也不会被收到,除these ones。
是的,您是通过Application Context
注册的,但可能是您的代码错误。
您应该通过Activity Context
而不是IntentFilter
进行注册,以避免内存泄漏。此外,尝试创建一个空格targetSdkVersion
并在之后添加您的操作。此代码在Android 8 Oreo
({1}}(Nexus 5X)IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.net.wifi.SCAN_RESULTS");
context.registerReceiver(new YourBroadcastReceiver(), intentFilter);
26+的应用程序中正常运行。
$TYPO3_CONF_VARS['FE']['pageNotFound_handling'] = 'index.php?id=35';
$TYPO3_CONF_VARS['FE']['pageNotFound_handling_statheader'] = 'HTTP/1.0 404 Not Found';
希望得到这个帮助。
答案 1 :(得分:2)
您的8.0 Android设备必须启用位置才能让wifimanager工作。
答案 2 :(得分:0)
您必须在运行时请求ACCESS_COARSE_LOCATION权限
@Override
public void onResume(){
super.onResume();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
if(checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 0);
}
}
}