我在使用 AlarmManager 调用的服务中有以下代码(这很好用)但是当我开始 wifi 扫描(返回 true)时,我从未在我创建的 BroadcastReceiver 中收到结果:
public class WifiCheckerService extends IntentService {
WifiManager wifiManager;
List<Wifi> savedWifis;
List<Wifi> wifisInRange;
List<ScanResult> wifisInRangeResults;
BroadcastReceiver broadcastReceiver;
public WifiCheckerService() {
super("WifiCheckerService");
}
@Override
public void onCreate() {
super.onCreate();
setScanAlwaysAvailable();
if(broadcastReceiver == null)
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
check();
}
};
if (wifiManager == null)
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
registerReceiver(broadcastReceiver,
new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
}
@Override
protected void onHandleIntent(Intent intent) {
if (intent != null) {
wifiManager.startScan();
}
}
private void check() {
// irrelevant stuff
Intent intent = new Intent(Constants.BROADCAST_UPDATE_WIFI);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
sendBroadcast(intent);
}
}
public List<Wifi> getSavedWifisInRange(List<Wifi> savedWifis,
List<ScanResult> wifisInRangeResults) {
List<Wifi> wifisInRange = new ArrayList<Wifi>();
for (Wifi wifi : savedWifis) {
for (int i = 0; i < wifisInRangeResults.size(); i++) {
if (wifi.getSsid().equals(wifisInRangeResults.get(i).SSID)) {
wifi.setRssi(String.valueOf(wifisInRangeResults.get(i).level));
wifisInRange.add(wifi);
}
}
}
return wifisInRange;
}
public void setScanAlwaysAvailable() {
/* Set scan always available if it is turned off*/
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) {
if (!wifiManager.isScanAlwaysAvailable()) {
startActivity(new Intent(WifiManager.ACTION_REQUEST_SCAN_ALWAYS_AVAILABLE));
}
}
}
public boolean isTetheringActive() {
//irrelevant stuff
return false;
}
private boolean isAnySavedWifiInRange() {
//irrelevant stuff
return true;
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(wifiScanReceiver);
}
}
我以前使用过带有 wifi 扫描的 BroadcastReceiver,但我不知道为什么这不起作用。
怎么了?
编辑:AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.android.vending.BILLING" />
<application
android:name=".WifiSentinelApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".activities.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activities.DonateActivity"
android:label="@string/donate"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".activities.SettingsActivity"
android:label="@string/settings"
android:screenOrientation="portrait" />
<receiver android:name=".receivers.AlarmReceiver" />
<receiver android:name=".receivers.BootReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".services.WifiCheckerService" />
</application>