2016-05-02 58 views

回答

0

这看起来像是this post的副本。但是相关的代码块在下面。这是一种捕捉错误并相应地更改用户界面的方法。

 webView.setWebViewClient(new WebViewClient() { 

     @Override 
     public void onReceivedError(final WebView view, int errorCode, String description, 
       final String failingUrl) { 
      //control you layout, show something like a retry button, and 
      //call view.loadUrl(failingUrl) to reload. 
      super.onReceivedError(view, errorCode, description, failingUrl); 
     } 
    }); 

您还可以使用广播接收器监听整个应用程序的网络连接丢失情况。你可以找到一个不错的写作here。但要点是你注册一个网络变更的接收器,然后你检查变化是否是断开的。然后,您可以使用自己的事件总线发送可更新UI的广播。

public class NetworkChangeReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(final Context context, final Intent intent) { 
    final ConnectivityManager connMgr = (ConnectivityManager) context 
      .getSystemService(Context.CONNECTIVITY_SERVICE); 

    final android.net.NetworkInfo wifi = connMgr 
      .getNetworkInfo(ConnectivityManager.TYPE_WIFI); 

    final android.net.NetworkInfo mobile = connMgr 
      .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 

    if (wifi.isAvailable() || mobile.isAvailable()) { 
     // Do something 

     Log.d("Network Available ", "Flag No 1"); 
    } 
    } 
} 

这里检查:

public boolean isOnline(Context context) { 

    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo netInfo = cm.getActiveNetworkInfo(); 
    //should check null because in air plan mode it will be null 
    return (netInfo != null && netInfo.isConnected()); 
} 
+0

这看起来有帮助..谢谢DonQuix,我会对其进行测试,并让你知道.. – emenpy

+0

如何重新连接时重新建立连接? – emenpy

相关问题