我使用下面的代碼,註冊一個觀察員的活動線程會導致應用程序變得unreponsive
活動:
//Register for network status updates
NetworkStatus networkStatus = new NetworkStatus(this);
networkStatus.addObserver(new OfflineActivity());
Thread thread = new Thread(networkStatus);
thread.run();
主題:
@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");
}
}
}
當我點擊進入活動,該程序變得沒有反應..任何想法?
嗯,無限循環生成notifyObservers()消息連接到互聯網? –
我有一個thread.sleep(),但我刪除它,因爲我認爲這是造成問題。問題是thread.run()而不是thread.start()。 – TomSelleck