我构建了一个使用 webview 流式传输音频的应用程序。现在我添加了一个检查是否有连接(或没有)。使用 toast 我会显示一条带有“true”或“false”的消息,但我只想在连接发生变化时才显示 toast。我该怎么办?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
update();
private void update() {
new Thread() {
public void run() {
while (true) {
runOnUiThread(new Runnable() {
@Override
public void run() {
isOnline();
}
});
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
}
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
Toast.makeText(getApplicationContext(), "true",Toast.LENGTH_LONG).show();
return true;
}
Toast.makeText(getApplicationContext(), "false",Toast.LENGTH_LONG).show();
return false;