我想不断检查互联网连接并更改TextView
连接是否出现或消失。但是在TextView
我刷新应用程序之前不会改变。这是我当前的代码:
TextView offline;
public void run() {
try {
Looper.prepare();
new Handler();
if(isInternetOn()) {
swebview.reload();
offline.setText(null);
}else{
swebview.reload();
offline.setText("No Internet Connection!");
}
Looper.loop();
} catch (Throwable t) {
Log.d(TAG, "halted due to an error", t);
}
}
和isInternetOn()
代码:
public final boolean isInternetOn() {
ConnectivityManager connec = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
// ARE WE CONNECTED TO THE NET
if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED ||
connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTING ||
connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING ||
connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED ) {
// MESSAGE TO SCREEN FOR TESTING (IF REQ)
//Toast.makeText(this, connectionType + ” connected”, Toast.LENGTH_SHORT).show();
return true;
} else if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED || connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED ) {
//System.out.println(“Not Connected”);
return false;
}
return false;
}
}