如何在选项卡式活动中的片段中使用wifimanager.getscanresults()?

时间:2017-12-09 09:30:30

标签: android android-fragments android-wifi android-tabbed-activity

我想在标签式活动中显示WiFi扫描的结果,其中包含2个标签' OM' &安培; ' OR&#39 ;.在' OM'选项卡,我想列出SSID以OM&开头的设备。 in' OR'以SSID OR开头的标签设备。 我尝试使用在活动public class MainActivity extends AppCompatActivity {}中正常工作的代码。但是它不会在片段中给出任何结果(返回null)。 请查看以下代码&让我知道我在做什么错。 提前谢谢。

public static class OmFragment extends Fragment {
    ListView listView;
    private static WifiManager wifiManager;
    private static WifiReceiver wifiReceiver;
    private static List<ScanResult> wifiList;
    ArrayList<String> arrayList = new ArrayList<>();
    ArrayAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_om_layout, container, false);
        listView = (ListView) view.findViewById(R.id.wifiDeviceList);
        wifiManager = (WifiManager) getActivity().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        wifiReceiver = new WifiReceiver();
        if (!wifiManager.isWifiEnabled()) {
            if (wifiManager.setWifiEnabled(true)) {
                Toast.makeText(getActivity(), "WIFI enabled.", Toast.LENGTH_SHORT).show();
                getActivity().registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
                wifiManager.startScan();
            } else {
                Toast.makeText(getActivity(), "Error in WiFi Initialization!!", Toast.LENGTH_SHORT).show();
            }
        } else if (wifiManager.isWifiEnabled()) {
            getActivity().registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
            wifiManager.startScan();
        }
        this.adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, arrayList);
        listView.setAdapter(this.adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                Toast.makeText(getActivity(), "Selected: " + listView.getItemAtPosition(position), Toast.LENGTH_LONG).show();
            }
        });
        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        try {
            getActivity().unregisterReceiver(wifiReceiver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    class WifiReceiver extends BroadcastReceiver {
        public void onReceive(Context c, Intent intent) {
            String action = intent.getAction();
            if (action.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
                Toast.makeText(getActivity(), "SCAN_RESULTS_AVAILABLE", Toast.LENGTH_SHORT).show();
                wifiList = wifiManager.getScanResults();
                Toast.makeText(getActivity(), "WiFi List: " + wifiList, Toast.LENGTH_SHORT).show();
                if (wifiList.size() > 0) {
                    int TotalWifiDevices = wifiList.size();
                    Toast.makeText(getActivity(), "Number of Wifi connections : " + TotalWifiDevices, Toast.LENGTH_SHORT).show();
                    try {
                        while (TotalWifiDevices >= 0) {
                            TotalWifiDevices--;
                            arrayList.add(wifiList.get(TotalWifiDevices).SSID);
                            adapter.notifyDataSetChanged();
                        }
                    } catch (Exception e) {
                        Log.w("WiFi Exception: ", e);
                    }
                } else {
                    Toast.makeText(getActivity(), "No Wifi found.", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我在Naugat上尝试App,我们需要提供以下权限

PostAddToCartwrong

&安培;必须打开位置以及给予位置许可。代码现在正在运作。