I have an activity which registers an Observer using the following code
Activity:
//Register for network status updates
NetworkStatus networkStatus = new NetworkStatus(this);
networkStatus.addObserver(new OfflineActivity());
Thread thread = new Thread(networkStatus);
thread.run();
Subject:
@Override
public void run() {
for(;;) {
Log.d("NetworkStatus", "Checking Network Status");
if(isConnectedToInternet(context)){
notifyObservers();
Log.d("NetworkStatus", "Network Connection is established");
}
else {
Log.d("NetworkStatus", "Not connected to network");
}
}
}
When I click into the activity, the program becomes unresponsive.. Any ideas?
You should be using
.start();when you explictly call the run method, it doesn’t actually spawn a new Thread, instead the method call still runs in the UI Thread.