我正在连接到我的应用程序中的一个wifi网络,当我这样做的时候,我向一个用户展示了一个带有动画的服装对话框。
问题是,当我做这行>
wifiManager.addNetwork(conf);
我在对话框中的动画被卡住了,有时我的应用程序会没有响应。
知道怎么解决这个问题吗?
如果我在连接到网络时不能显示对话框,您建议给用户看什么?
这是我在Wifi包装器类中连接到网络的功能:
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
}
发布于 2016-05-10 11:27:08
您正在连接到UI线程上的WiFi,这会停止它,直到代码完成。如果要运行动画,应该将连接代码放入AsyncTask
中,或者以其他方式将其从UI线程中移出。
https://stackoverflow.com/questions/37137032
复制相似问题