wifi 管理器 wifiManager.addNetwork 停止我的 UI

wifi Manager wifiManager.addNetwork Stops my UI

我正在我的应用程序中连接到 wifi 网络 在我这样做的同时,我向用户展示了一个包含动画的服装对话框。

问题是当我执行此行时 >

wifiManager.addNetwork(conf);

我在对话框中的动画卡住了,有时我的应用程序没有响应。

知道如何解决这个问题吗?

如果我在连接到网络时无法显示对话框,您建议向用户显示什么?

这是我的 Wifi Wrapper 中的连接网络功能 class :

public void connectToNetwork(String ssidName, String netPassword, final Context applicationContext, final wifiConnectionEsteblishedInterface callback) { // in order to call callback from another thread it needs to be final

        final String networkSSID = ssidName;
        String networkPass = netPassword;

        WifiConfiguration conf = new WifiConfiguration();
        conf.SSID = "\"" + networkSSID + "\"";   // Please note the quotes. String should contain
        conf.preSharedKey = "\"" + networkPass + "\"";

        WifiManager wifiManager = (WifiManager) applicationContext.getSystemService(Context.WIFI_SERVICE);
        wifiManager.addNetwork(conf);

        List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
        for (WifiConfiguration i : list) {

            if (i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {

                wifiManager.disconnect();
                wifiManager.enableNetwork(i.networkId, true);
                wifiManager.reconnect();
                break;
            }
        }

        Log.d("myLogs", "log-0");

        callback.callbackFromWifi(); // calls the cllback


    }

这是我的对话侦听器

public void onClick(DialogInterface dialog, int id) { //on button clicked

                        mainDialog.show(); //show main dialog back again

                        loaderScreenMainText.setText("Connecting to RADWIN WiFi"); // shows on screen message

                        final Handler handler = new Handler();
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                // Do something after 5s = 5000ms
                                wifiWrapper WifiWrapper = new wifiWrapper();

                                WifiWrapper.toggleWiFi(Scanning_Barcode_Activity.this, true); // turns wifi ON

                                aligmentManager aligmentManager = new aligmentManager(); // tells the manager that we started looking for a wifi
                                aligmentManager.initlizedConnectingToWifi();


                                if (scanResults != null) { //in future use we will use scanResults veriable which is already initlized by now;

                                    WifiWrapper.connectToNetwork("nmsRoom", "12345678", getApplication(), callbackFunctionForisWifiConnection); //in the future we will pass the scan results to the network

                                    //

                                } else {
                                    //do something in case scan result fails

                                }


                            }
                        }, 500); //deley in order to start the animation before hand (otherwise the dialog shows after, 2, 3 seconds


                    }

您正在 UI 线程上连接到 WiFi,该线程会停止它直到代码完成。如果您希望动画 运行,您应该将连接代码放入 AsyncTask,或者以其他方式将其移出 UI 线程。