기본적으로 주어진 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();
}
중복 가능성 (http://stackoverflow.com/questions/6552160/prevent-webview-from-displaying-web-page-not-available) – Darpan