我知道有很多例子,但是我仍然无法使它工作。
当电话连接到适当的wifi时,我需要监视布尔值,该值会更改为True。 Wifi连接并在第二个线程中进行检查。也许有问题?我已经尝试了许多解决方案,但无法完成。
变量的包装器类:
import java.util.ArrayList;
import java.util.List;
public class ConnectivityStatus {
private Boolean status = Boolean.FALSE;
private ConnectivityListener listener;
public Boolean getStatus(){
return status;
}
public void setStatus(Boolean status){
this.status = status;
if(status) {
listener.onChange();
}
}
public void addConnectivityListener(ConnectivityListener l) {
this.listener = l;
}
interface ConnectivityListener{
void onChange();
}
}
MainActivity:
public class MainActivity extends AppCompatActivity {
...
private ConnectivityStatus mConnectionStatus;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
mConnectionStatus = new ConnectivityStatus();
...
connectButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startLoadingScreen();
connectToCamera(mWifiManager);
new Thread(new Runnable() {
public void run() {
long startTime = System.currentTimeMillis();
for(int i = 0; i<=6; i++) {
mConnectionStatus.setStatus(checkWifiSsid(mWifiManager, startTime));
if(mConnectionStatus.getStatus()) {
break;
}
}
}
});
}
});
mConnectionStatus.addConnectivityListener(new ConnectivityStatus.ConnectivityListener() {
@Override
public void onChange(){
openWebView();
}
});
}
答案 0 :(得分:1)
我之前没有注意到,但是,是的,您在线程中缺少了一些东西。您正在创建一个新线程,但没有告诉它启动:
new Thread(new Runnable(){
public void run(){
// Optionally, you can also use log messages for debugging
Log.d("MY_LOG_TAG", "Some message to look for in the log.");
// ...
}
}).start(); // Make sure to tell it to start