8.2 Android

优质
小牛编辑
126浏览
2023-12-01

互动广告安卓代码接入范例

/**
 * 在webview加载url前,对webview进行设置的代码如下。
 */
public void initWebView(WebView mWebView, String url){

    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    webSettings.setBlockNetworkImage(false);
    webSettings.setDomStorageEnabled(true);
    /**
     * 仅缓存数据
     */
    webSettings.setAppCacheEnabled(false);
    webSettings.setDatabaseEnabled(true);

    webSettings.setUseWideViewPort(true);
    webSettings.setLoadWithOverviewMode(true);
    webSettings.setTextSize(WebSettings.TextSize.NORMAL);

    //缓存
    webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
    webSettings.setAppCacheMaxSize(1024 * 1024);
    String appCacheDir = this.getDir("cache", Context.MODE_PRIVATE).getPath();
    webSettings.setDatabasePath(appCacheDir);

    webSettings.setSupportZoom(false);// 支持缩放
    webSettings.setBuiltInZoomControls(false);
    webSettings.setDisplayZoomControls(false);// 取消显示缩放按钮
    webSettings.setSupportMultipleWindows(true);// 支持多窗口
    webSettings.setSavePassword(false);
    mWebView.removeJavascriptInterface("searchBoxJavaBridge_");
    mWebView.removeJavascriptInterface("accessibility");
    mWebView.removeJavascriptInterface("accessibilityTraversal");
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
    }

    mWebView.setDownloadListener(new DownloadListener() {
        @Override
        public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype,long contentLength) {
            Uri uri = Uri.parse(url);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
    });
    mWebView.setWebViewClient(new WebViewClient() {
                                  @Override
                                  public void onPageFinished(WebView view, String url) {
                                      Log.e("onPageFinished", url);
                                  }
                                  @Override
                                  public void onPageStarted(WebView view, String url, Bitmap favicon) {
                                      Log.e("onPageStarted", url);
                                  }
                                  @Override
                                  public boolean shouldOverrideUrlLoading(WebView view, String url) {
                                      // 可拦截所以页面内跳转
                                      Log.e("shouldOverrideUrlLoad", url);
                                      //boolean doUrl = false;
                                      view.loadUrl(url);
                                      return true;
                                  }
                              });

      mWebView.loadUrl(url);
}