我如何从一个片段中启用或禁用 wifi

how can i enable or disable wifi from a fragment

你好,请问你能帮帮我吗?我正在尝试从片段中关闭或关闭 wifi。它给出了错误。是否可以在片段中进行此操作,或者只能在 activity 中进行?请帮助或建议替代方法

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

}

我使用 ToggleButton 而不是 Switch 实现了相同的功能。另外,我在片段中使用自己的私有内部 class WifiScanReceiver,一旦收到 wifi 信号,它就会用来做一些事情。

WifiScanReceiver Class 的代码;将此代码保留在您的片段中。

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

// 这是您在 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
                }
            }
        });