android - 无法在 Android 应用程序中定位 wifi 网络

标签 android android-wifi

public class MainActivity extends AppCompatActivity {

WifiManager wifi;
WifiReceiver wifiReceiver;
StringBuilder sb = new StringBuilder();
TextView t ;
List<ScanResult> wifiList;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    wifi.startScan();
    t=(TextView) findViewById(R.id.text);
  //  t.setText("Scanning....");
}


class WifiReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
            wifiList = wifi.getScanResults();
            sb.append("\nNumber Of Wifi connections :"+wifiList.size()+"\n\n");
            for(int i = 0; i < wifiList.size(); i++){

                sb.append(new Integer(i+1).toString() + ". ");
                sb.append((wifiList.get(i)).toString());
                sb.append("\n\n");
            }
         t.setText(sb);
    }
}
}

我想在列表中显示 wifi 网络。我写了这段代码,但屏幕上没有显示任何内容。有人可以指出我哪里出错了。

谢谢!

最佳答案

你遗漏了一件事,你需要初始化 WifiReceiver 引用:

wifiReceiver = new WifiReceiver();

我刚刚使用此更改对其进行了测试,它对我有效。

我在 fragment 中使用了它:

public class BlankFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    WifiManager wifi;
    WifiReceiver wifiReceiver;
    StringBuilder sb = new StringBuilder();
    TextView t ;
    List<ScanResult> wifiList;

    public static BlankFragment newInstance(String param1) {
        BlankFragment fragment = new BlankFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    public BlankFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);

        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_blank, container, false);

        wifiReceiver = new WifiReceiver(); //added

        wifi = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
        getActivity().registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        wifi.startScan();
        t=(TextView) rootView.findViewById(R.id.text);
        return rootView;
    }

    class WifiReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            wifiList = wifi.getScanResults();
            sb.append("\nNumber Of Wifi connections :"+wifiList.size()+"\n\n");
            for(int i = 0; i < wifiList.size(); i++){

                sb.append(new Integer(i+1).toString() + ". ");
                sb.append((wifiList.get(i)).toString());
                sb.append("\n\n");
            }
            t.setText(sb);
        }
    }

}

一定要设置正确的权限:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

结果:

enter image description here

关于android - 无法在 Android 应用程序中定位 wifi 网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32986923/

相关文章:

android - 安卓:elevation tag raises issue in android layout file

android - 在响应式网站上检测移动设备的最大视频分辨率支持

Android:如何:显示带有移动当前位置指针的 map (静止图像文件)

android - 如何在 android 应用程序中提供选择 wi-fi 或 GPRS 网络连接的选项

android - Android连接WIFI后如何通过移动网络保持连接?

安卓 - java.lang.RuntimeException : Unable to instantiate activity

android - DalvikVM 上的 CLI 在 JNI 库上失败

android - 带有 bundle 的 android SDK 的热敏打印机?

android - Leanback 焦点问题

android - 可视化wifi信号强度