1

我在使用 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>

4

1 回答 1

0

我对 IntentService 的理解是,它只会在处理请求所需的时间(即运行 onHandleIntent() 所需的时间)内处于活动状态。我没有测试你的代码,但我的猜测是你的广播接收器在 onDestroy() 运行时被立即取消注册,在扫描结果交付之前。可能 IntentService 不适合这个。您可能需要创建“常规”(长期背景)服务。或者,也许让您的 BroadcastReceiver 在您将显示扫描结果并完全摆脱这个 IntentService 的活动中更有意义。WifiManager::startScan() 是异步的,因此无论如何都不需要在后台服务上调用它。

于 2015-12-29T06:04:57.177 回答