Search code examples
javaandroidandroid-fragmentsandroid-wifiwifimanager

how can i enable or disable wifi from a fragment


hello can you help me please. I am trying to turn or off the wifi from a fragment. it is giving errors. Is it possible to make this operation in a fragment or it can only be done in an activity? please help or suggest alternative ways

package fragments;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.example.amhomeautomationprototype.R;

public class SettingFragment extends Fragment{

    Switch wifi;
    TextView displayStatus;

    @Override
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_setting, container, false);

        wifi =(Switch)v.findViewById(R.id.wifiSwitch);
        displayStatus=(TextView)v.findViewById(R.id.textView1);

        final WifiManager wifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);


        wifi =(Switch)v.findViewById(R.id.outSwitch);
        wifi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                  if (isChecked) {
                      if(wifiManager.isWifiEnabled()) {
                        wifiManager.setWifiEnabled(false);
                        displayStatus.setText("Wifi enabled !!!");
                    }
                  } else {
                      if(!wifiManager.isWifiEnabled()) {
                        wifiManager.setWifiEnabled(true);
                        displayStatus.setText("Wifi disabled !!!");
                    }
                  }
              }
          });

        return v;
    }

}

Solution

  • I Implemented the same using ToggleButton instead of Switch. Also, i am using my own private inner class WifiScanReceiver inside fragment which is used to do stuff once the wifi signal is received.

    Code for WifiScanReceiver Class; Keep this code in your fragment.

     private class WifiScanReceiver extends BroadcastReceiver {
        public void onReceive(Context c, Intent intent) {
            List<ScanResult> wifiScanList = wifi.getScanResults();
            // Do whatever you wish to do with ScanResult
        }
    }
    

    // here is your code inside onCreateView.

     wifi=(WifiManager)getActivity().getSystemService(Context.WIFI_SERVICE);
     WifiScanReceiver wifiReciever =  new WifiScanReceiver();
    
            ToggleButton toggle = (ToggleButton) this.view.findViewById(R.id.toggleButton);
    
            toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        getActivity().registerReceiver(wifiReciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
                        wifi.startScan();
                    } else {
                        wifi.disconnect();
                        getActivity().unregisterReceiver(wifiReciever);
                        // Set your Adapters Here
                    }
                }
            });