带wifi扫描的广播接收器不工作

时间:2015-12-28 17:28:38

标签: android service android-broadcastreceiver

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

}

我之前使用过BroadcastReceiver的wifi扫描,但我不知道为什么这不起作用。

怎么了?

编辑: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>

1 个答案:

答案 0 :(得分:0)

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