使用BroadcastReceiver检测状态更改无效

时间:2018-05-17 07:00:35

标签: android broadcastreceiver

我正在尝试使用wifi检测mobile databluetoothlocationbroadcastreceiver状态更改。我就是这样做的:

    StateChangeReceiver stateChangeReceiver = new StateChangeReceiver();

    WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    ConnectivityManager conManager = (ConnectivityManager)getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    LocationManager locationManager = (LocationManager)getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

@Override
protected void onStart() {
    super.onStart();

    IntentFilter bluetooth = new IntentFilter(bluetoothAdapter.ACTION_STATE_CHANGED);
    IntentFilter wifi = new IntentFilter(wifiManager.WIFI_STATE_CHANGED_ACTION);
    IntentFilter gps = new IntentFilter(locationManager.PROVIDERS_CHANGED_ACTION);
    IntentFilter data = new IntentFilter(conManager.CONNECTIVITY_ACTION);
    registerReceiver(stateChangeReceiver, bluetooth);
    registerReceiver(stateChangeReceiver, wifi);
    registerReceiver(stateChangeReceiver , gps);
    registerReceiver(stateChangeReceiver , data);
}

@Override
protected void onStop() {
    super.onStop();
    unregisterReceiver(stateChangeReceiver);
}

 private class  StateChangeReceiver extends BroadcastReceiver  {
    @Override
    public void onReceive (Context context, Intent intent) {
        if (intent.getAction().equals(wifiManager.WIFI_STATE_CHANGED_ACTION)) {
            WifiStateChanged(intent);
        } else if(intent.getAction().equals(bluetoothAdapter.ACTION_STATE_CHANGED)) {
            BluetoothStateChanged(intent);
        } else if(intent.getAction().equals(locationManager.PROVIDERS_CHANGED_ACTION)) {
            GpsSwitchStateChanged(context , intent);
        }  else if(intent.getAction().equals(conManager.CONNECTIVITY_ACTION)) {
            DataStateChanged(intent);
        }
    }
};

private void WifiStateChanged (Intent intent){
     int wifiStateExtra = intent.getIntExtra(wifiManager.EXTRA_WIFI_STATE, wifiManager.WIFI_STATE_UNKNOWN);
     if (wifiStateExtra == wifiManager.WIFI_STATE_ENABLED)
            ((ImageView) launcher.findViewById(R.id.wifi_button)).setImageResource(R.drawable.ic_wifi_orange_24dp);
     else if (wifiStateExtra == wifiManager.WIFI_STATE_DISABLED)
            ((ImageView) launcher.findViewById(R.id.wifi_button)).setImageResource(R.drawable.ic_wifi_white_24dp);
};

private void DataStateChanged (Intent intent) {
    NetworkInfo netInfo = conManager.getActiveNetworkInfo();

    if (netInfo != null && netInfo.getType() == ConnectivityManager.TYPE_MOBILE)
        ((ImageView) launcher.findViewById(R.id.data_button)).setImageResource(R.drawable.ic_data_orange_24dp);
    else
        ((ImageView) launcher.findViewById(R.id.data_button)).setImageResource(R.drawable.ic_data_white_24dp);
};

private void BluetoothStateChanged (Intent intent){
    String action = intent.getAction();
    if (bluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
        if(intent.getIntExtra(bluetoothAdapter.EXTRA_STATE, -1) == bluetoothAdapter.STATE_ON)
            ((ImageView) launcher.findViewById(R.id.bluetooth_button)).setImageResource(R.drawable.ic_bluetooth_orange_24dp);
        else if(intent.getIntExtra(bluetoothAdapter.EXTRA_STATE, -1) == bluetoothAdapter.STATE_OFF)
            ((ImageView) launcher.findViewById(R.id.bluetooth_button)).setImageResource(R.drawable.ic_bluetooth_white_64dp);

    }
};

private void GpsSwitchStateChanged (Context context,Intent intent){
    int locationMode=0;
    try
    {
        locationMode = android.provider.Settings.Secure.getInt(context.getContentResolver(), android.provider.Settings.Secure.LOCATION_MODE);

    } catch (android.provider.Settings.SettingNotFoundException e)
    {
        e.printStackTrace();
    }

    if (locationMode == android.provider.Settings.Secure.LOCATION_MODE_SENSORS_ONLY
            || locationMode == android.provider.Settings.Secure.LOCATION_MODE_HIGH_ACCURACY
            ||locationMode == android.provider.Settings.Secure.LOCATION_MODE_BATTERY_SAVING )
        ((ImageView) launcher.findViewById(R.id.location_button)).setImageResource(R.drawable.ic_location_orange_24dp);
    else if (locationMode == android.provider.Settings.Secure.LOCATION_MODE_OFF) {
        ((ImageView) launcher.findViewById(R.id.location_button)).setImageResource(R.drawable.ic_location_white_24dp);
    }
};

我还添加了允许,但我需要:

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<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_LOCATION" />

但跟踪的唯一更改是wifi状态更改,其余更改不会触发StateChangeReceiver。我很困惑,为什么它不起作用?

2 个答案:

答案 0 :(得分:0)

尝试仅使用具有所有操作的唯一IntentFilter注册一个BroadcastReceiver。例如:

IntentFilter intentFilter = new IntentFilter(); 
intentFilter.addAction(bluetoothAdapter.ACTION_STATE_CHANGED);
intentFilter.addAction(wifiManager.WIFI_STATE_CHANGED_ACTION);
intentFilter.addAction(locationManager.PROVIDERS_CHANGED_ACTION);
intentFilter.addAction(conManager.CONNECTIVITY_ACTION);

registerReceiver(stateChangeReceiver, intentFilter); 

通过这种方式,您可以在Manifest中模拟BroadcastReceiver的声明:

    <receiver
        android:name="YourReceiver">
        <intent-filter>
            <action android:name="action1"/>
            <action android:name="action2"/>
        </intent-filter>
    </receiver>

另一个解决方案是为每个动作创建一个BroadcastReceiver,因为我看到每个动作都有一个方法来解析它。创建4个BroadcastReceivers并在其BroadcastReceiver中执行每个动作的逻辑并不是很疯狂。

答案 1 :(得分:0)

您正在多次调用registerReceiver,这导致只有最后一行执行尝试使用

IntentFilter intentFilter = new IntentFilter(); 
intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
intentFilter.addAction(LocationManager.PROVIDERS_CHANGED_ACTION);
intentFilter.addAction(ConnectionManager.CONNECTIVITY_ACTION);

registerReceiver(mReceiver, intentFilter); 

您只能在一个接收器中侦听操作,并且可以按操作提取 例如:

   private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();

            if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
               .....
            } else if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
               ...
            }
        }
    };