Home Detecting state changes using BroadcastReceiver is not working
Reply: 0

Detecting state changes using BroadcastReceiver is not working

user5830
1#
user5830 Published in May 27, 2018, 3:32 am

I am trying to detect wifi, mobile data, bluetooth and location state changes using broadcastreceiver. this is how i do it:

    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);
    }
};

I also added permission that i though was needed:

<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" />

But the only changes that are tracking is wifi state changes and the rest doesn't fire the StateChangeReceiver. I am pretty confused why it's not working?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.307254 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO