2012-10-26 2 views
0

내 ContentView에 오프라인 콘텐츠와 WebView가 있습니다. 장치가 온라인 상태 일 때 WebView는 웹의 컨텐트를 표시합니다. 그러나 장치가 오프라인 일 때 대신 흰색 페이지가 표시됩니다. 이 빈 페이지를 제거하려면 어떻게해야합니까? WebView 콘텐트를 오프라인 모드로 표시하지 않습니다.Android WebView는 오프라인 모드에서 빈 페이지를 표시합니다.

내 코드는 다음과 같습니다

 ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
    android.net.NetworkInfo wifi = cm 
      .getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
    android.net.NetworkInfo datac = cm 
      .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 
    if ((wifi != null & datac != null) 
      && (wifi.isConnected() | datac.isConnected())) { 
    android.webkit.WebView wv = 
    (android.webkit.WebView)this.findViewById(R.id.myWebView); 
    wv.clearCache(true); 
    wv.loadUrl("MyURL"); 
    wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); 
    wv.getSettings().setJavaScriptEnabled(true); 
    } 
+0

이 참조 http://stackoverflow.com/a/14671256/3981656 –

답변

0

나는 당신이 대신 빈 페이지의 일부 오류를 표시 할 수정 이해하고 잘합니다. 그런 다음 아래 코드를 사용할 수 있습니다

mainWebView.setWebViewClient(new WebViewClient() { 
     public void onReceivedError(WebView view, int errorCode, 
       String description, String failingUrl) { 
      String summary = "<html><h1>Could not connect to the server</h1><h2>May be you are not connected to Internet or</h2><h3>our server is down.</h3><body>Please exit the program and try again later.</body></html>"; 
      mainWebView.loadData(summary, "text/html", null);    

     } 
    }); 

또는 장치가 아닌 그럼 그냥이 코드를 사용하는 빈 페이지를 보여주는의 오프라인 때 다시 같은 활동에 함께 가고 싶은 경우 :

    mainWebView.onResume(); 
        finish(); 

나는 똑같은 문제가 있었다. 그래서 빈 페이지 대신 오류를 표시했습니다. 그 후 나는 내 웹을 시작한 곳으로 돌아가기로 결심했다. 그래서 나는 finish()를 사용했다; . 이 오류를 완료하고 돌아갈 수 있습니다.

내가 사용한 소스 코드가 있습니다.

mainWebView.setWebViewClient(new WebViewClient() { 
      public void onReceivedError(WebView view, int errorCode, 
        String description, String failingUrl) { 
       String summary = "<html><h1>Could not connect to the server</h1><h2>May be you are not connected to Internet or</h2><h3>our server is down.</h3><body>Please exit the program and try again later.</body></html>"; 
       mainWebView.loadData(summary, "text/html", null); 

       Toast.makeText(activity, "Error. Cannot to the server. May be you are not connected to Internet or our server is down or " + description, Toast.LENGTH_LONG) 
         .show(); 
       mainWebView.onResume(); 
        finish(); 



      } 
     }); 

     mainWebView.loadUrl(getIntent().getStringExtra("url")); 

피씨 늦게 대답합니다. 나는 1 년 후에 믿는다.

관련 문제