2014-11-21 4 views
1

기본적으로 주어진 URL을로드하는 레이아웃의 WebView가 있습니다. 모든 것이 잘 작동합니다. 그러나 WebView가 서버에 액세스 할 수 없거나 웹 페이지가 제거 된 것과 같은 이유로 인해 페이지를 사용할 수없는 경우 WebView에 "웹 페이지를 사용할 수 없습니다 .www.givenurl_something.com의 웹 페이지가 일시적으로 다운 될 수 있습니다"등이 표시됩니다. 이 텍스트를 사용자에게 표시하는 대신 WebView 내부에 앱 자체에 저장된 기본 이미지를 표시하려고합니다. 모든 것이 잘 작동하면 웹 페이지가 열려야합니다. 그렇지 않으면, 기본 이미지가 표시되어야합니다, 아무도 나를 도울 수 있습니까? 다음은 제 코드입니다.웹 페이지를로드 할 수없는 경우 webview에 기본 이미지를 표시하는 방법

public class WebviewActivity extends Activity { 
// flag for Internet connection status 
Boolean isInternetPresent = false; 

// Connection detector class 
ConnectionDetector cd; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    getWindow().requestFeature(Window.FEATURE_PROGRESS); 

    setContentView(R.layout.webview); 
    cd = new ConnectionDetector(getApplicationContext()); 

    // Create ad request 
    isInternetPresent = cd.isConnectingToInternet(); 
    if (isInternetPresent) { 

     WebView w1 = (WebView) findViewById(R.id.webView1); 
     w1.setWebViewClient(new WebViewClient()); 
     w1.getSettings().setJavaScriptEnabled(true); 
     w1.getSettings().setBuiltInZoomControls(true); 

     w1.loadUrl("http://www.google.com"); 
     WebClientClass webViewClient = new WebClientClass(); 
     w1.setWebViewClient(webViewClient); 
     WebChromeClient webChromeClient = new WebChromeClient(); 
     w1.setWebChromeClient(webChromeClient); 
    } 

    else { 

     showAlertDialog(WebviewActivity.this, "No Internet Connection", 
       "You don't have internet connection.", false); 
    } 
} 

public class WebClientClass extends WebViewClient { 
    ProgressDialog pd = null; 

    @Override 
    public void onPageStarted(WebView view, String url, Bitmap favicon) { 
     super.onPageStarted(view, url, favicon); 
     pd = new ProgressDialog(WebviewActivity.this); 
     pd.setTitle("Please wait"); 
     pd.setMessage("Page is loading.."); 
     pd.show(); 
    } 

    @Override 
    public void onPageFinished(WebView view, String url) { 
     super.onPageFinished(view, url); 
     pd.dismiss(); 
    } 

} 

public class WebChromeClass extends WebChromeClient { 
} 

public void showAlertDialog(Context context, String title, String message, 
     Boolean status) { 
    AlertDialog alertDialog = new AlertDialog.Builder(context).create(); 

    // Setting Dialog Title 
    alertDialog.setTitle(title); 

    // Setting Dialog Message 
    alertDialog.setMessage(message); 

    // Setting alert dialog icon 
    // alertDialog.setIcon(R.id.action_mode_close_button); 

    // Setting OK Button 
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      finish(); 
     } 
    }); 

    // Showing Alert Message 
    alertDialog.show(); 
} 
+0

중복 가능성 (http://stackoverflow.com/questions/6552160/prevent-webview-from-displaying-web-page-not-available) – Darpan

답변

1

사용이 코드를 사용자의 오류 페이지를 무시하고 자신의 사용자 지정 페이지를로드 할 수 있습니다. 그리고이 사용자 정의 오류 페이지 (myerrorpage.html)를 사용자 정의 이미지와 함께 assets 폴더에 보관하십시오.

mWebView.setWebViewClient(new WebViewClient() { 
      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
       mWebView.loadUrl("file:///android_asset/myerrorpage.html"); 

      } 
     }); 
[ "를 사용할 수없는 웹 페이지"를 표시하는 것을 차단 웹보기]의
1

당신은
onReceivedError (웹보기보기, INT의 errorCode를, 문자열 설명, 문자열 failingUrl)를 WebViewClient 클래스의
기능을 재정의 할 수 있습니다.
페이지를로드하는 동안 webview에서 오류가 발생하면 기본 이미지를 표시 할 수 있습니다. 오류 코드를 검사하여 다른 실패 사례에 대해 다른 이미지를로드 할 수도 있습니다.

상수 오류 코드에 대한 자세한 내용은 WebViewClient에 대한 안드로이드 문서를 참조하십시오 http://developer.android.com/reference/android/webkit/WebViewClient.html

관련 문제