android webview无网络情况下的处理

import com.yck.utils.diy.LoadingDialog;
import com.yck.utils.net.HttpUrl;
import com.yck.utils.tools.MyLog;

import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import cn.ijgc.goldplus.MyApplication;
import cn.ijgc.goldplus.R;
import cn.ijgc.goldplus.finance.ui.FinanceNxbIntroduceActivity;

public class DiscoverInformationFragment extends Fragment {

   private static final String TAG = FinanceNxbIntroduceActivity.class.getSimpleName();
   private String errorHtml = "";
   WebView webView;

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      return inflater.inflate(R.layout.discover_information, container, false);
   }

   @Override
   public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
      errorHtml = "

"
; webView = (WebView) getView().findViewById(R.id.web_include_webview); webView.getSettings().setDefaultTextEncodingName("UTF-8"); webView.getSettings().setJavaScriptEnabled(true); initWebView(); webView.loadUrl(HttpUrl.appInfo); } private void initWebView() { // 设置WebView属性,能够执行JavaScript脚本 webView.getSettings().setJavaScriptEnabled(true); // 设置不要缓存 webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); // 如果要播放Flash,需要加上这一句 // webView.getSettings().setPluginsEnabled(true); WebSettings ws = webView.getSettings(); ws.setSupportZoom(true); // 设置可以支持缩放 // ws.setUseWideViewPort(true); ws.setLoadWithOverviewMode(true); webView.setWebViewClient(new MyWebViewClient()); } class MyWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { MyLog.e(TAG, "shouldOverrideUrlLoading"); //view.loadUrl(url); return super.shouldOverrideUrlLoading(view, url); //return true; } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { MyLog.e(TAG, "onPageStarted"); super.onPageStarted(view, url, favicon); //showLoadingDialog(); } @Override public void onPageFinished(WebView view, String url) { MyLog.e(TAG, "onPageFinished"); //closeLoadingDialog(); super.onPageFinished(view, url); } @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { super.onReceivedError(view, errorCode, description, failingUrl); view.loadData(errorHtml, "UTF-8", "text/html"); } } /** * 关闭进度条 */ public void closeLoadingDialog() { if (loadDialog != null) { loadDialog.dismiss(); } } /** * 加载进度条 */ LoadingDialog loadDialog = null; public void showLoadingDialog() { if (loadDialog == null && this.getActivity() != null && !this.getActivity().isFinishing()) { loadDialog = new LoadingDialog(getActivity()); loadDialog.setInverseBackgroundForced(false); loadDialog.setCanceledOnTouchOutside(false); loadDialog.setOnCancelListener(new OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { dialog.dismiss(); MyApplication.getInstance().cancelPendingRequests(TAG); } }); } if(loadDialog != null){ loadDialog.show(); } } }

你可能感兴趣的:(Android)