webview与h5交互大全孚睿科技·ANDROID知识总结WebView

怎样抽取一个常用的WebView基类

2016-09-23  本文已影响576人  安卓小生

怎样抽取一个常用的WebView基类

}

* 第二步,需要定义一个方法,初始化这个fragment并且传递这个参数供WebView使用,如:
```java
public static BaseWebViewFragment newInstance(ShareModel model) {
     BaseWebViewFragment fragment = new BaseWebViewFragment();
     fragment.setArguments(model);
     return fragment;
 }
private class InsideWebChromeClient extends WebChromeClient {
      private View mCustomView;
      private CustomViewCallback mCustomViewCallback;
      //播放网络视频时全屏会被调用的方法
      @Override
      public void onShowCustomView(View view, CustomViewCallback callback) {
          super.onShowCustomView(view, callback);
          if (mCustomView != null) {
              callback.onCustomViewHidden();
              return;
          }
          mCustomView = view;
          video_view.addView(mCustomView);
          mCustomViewCallback = callback;
          ll_title.setVisibility(View.GONE);
          webView.setVisibility(View.GONE);
          getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
      }
      //视频播放退出全屏会被调用的
      public void onHideCustomView() {
          webView.setVisibility(View.VISIBLE);
          if (mCustomView == null) {
              return;
          }
          ll_title.setVisibility(View.VISIBLE);
          mCustomView.setVisibility(View.GONE);
          video_view.removeView(mCustomView);
          mCustomViewCallback.onCustomViewHidden();
          mCustomView = null;
          getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
          super.onHideCustomView();
      }
  }
WebSettings settings = webView.getSettings();
     settings.setJavaScriptEnabled(true);
     settings.setJavaScriptCanOpenWindowsAutomatically(true);
     settings.setPluginState(WebSettings.PluginState.ON);
     settings.setAllowFileAccess(true);
     settings.setLoadWithOverviewMode(true);
     settings.setUseWideViewPort(true);
     settings.setDomStorageEnabled(true);

     webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
     webView.loadUrl(model.getUrl());

     webView.setWebChromeClient(new InsideWebChromeClient());

     webView.setWebViewClient(new WebViewClient() {
         @Override
         public void onFormResubmission(WebView view, Message dontResend, Message resend) {
             super.onFormResubmission(view, dontResend, resend);
         }

         @Override
         public boolean shouldOverrideUrlLoading(WebView view, final String url) {
             if (url.startsWith("tel:")) {
                 RxPermissions.getInstance(getContext()).request(
                         Manifest.permission.CALL_PHONE)
                         .subscribe(new Action1<Boolean>() {
                             @Override
                             public void call(Boolean granted) {
                                 if (granted) {
                                     Uri uri = Uri.parse(url);
                                     Intent intent = new Intent("android.intent.action.DIAL", uri);
                                     startActivity(intent);
                                 } else {
                                     onShowMissingPermissionView();
                                 }
                             }
                         });
             } else {

             }
             //电话的时候这样处理,解决从电话界面回来,界面错误问题
             if (url.startsWith("tel:")) {
                 Log.i(TAG, "shouldOverrideUrlLoading" + url);
//
                 return true;
             }

             //解决不能goback的问题
             return false;
         }

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

             if (dialog != null || dialog.isShowing()) {
                 dialog.dismiss();
             }
             if (view.canGoBack()) {
                 if (ivClose != null) {
                     ivClose.setVisibility(View.VISIBLE);
                 }
             } else {
                 if (ivClose != null) {
                     ivClose.setVisibility(View.GONE);
                 }

             }
         }

         @Override
         public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
             super.onReceivedError(view, request, error);
             if (dialog != null || dialog.isShowing()) {
                 dialog.dismiss();
             }
             if (view.canGoBack()) {
                 if (ivClose != null) {
                     ivClose.setVisibility(View.VISIBLE);
                 }
             } else {
                 if (ivClose != null) {
                     ivClose.setVisibility(View.GONE);
                 }
             }
         }
     });

     webView.setDownloadListener(new DownloadListener() {
         @Override
         public void onDownloadStart(String url, String userAgent,
                                     String contentDisposition, String mimetype, long contentLength) {
             Log.d(TAG, "onDownloadStart: " + url);

             Uri uri = Uri.parse(url);
             Intent intent = new Intent(Intent.ACTION_VIEW, uri);
             startActivity(intent);
         }
     });

抽取WebViewFragment结束。

上一篇下一篇

猜你喜欢

热点阅读