如何在webview中阻止或避免弹出窗口?

时间:2016-11-23 17:51:21

标签: android webview popup

网页上有一些弹出式广告有什么方法可以防止弹出窗口加载时弹出窗口加载主站点没有出现是否有任何方法加载弹出窗口的主页面如何添加下载处理程序l意味着webview应支持下载.torrent文件

public class MainActivity extends AppCompatActivity {
    private WebView webView;
    private ProgressBar progressBar;
    private LinearLayout layoutProgress;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView = (WebView) findViewById(R.id.webView);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        layoutProgress = (LinearLayout) findViewById(R.id.layoutProgress);
        webView.setVisibility(View.GONE);
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setBuiltInZoomControls(true);
        settings.setSupportZoom(true);
        settings.setDisplayZoomControls(false);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                webView.setVisibility(View.VISIBLE);
                layoutProgress.setVisibility(View.GONE);
                progressBar.setIndeterminate(false);
                super.onPageFinished(view, url);

            }
            public void but(View v){

            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                layoutProgress.setVisibility(View.VISIBLE);
                progressBar.setIndeterminate(true);
                super.onPageStarted(view, url, favicon);
            }
        });
        if(isOnline()) {
            webView.loadUrl("http://testsite.com/");
        } else {
            String summary = "<html><body><font color='red'>No Internet Connection</font></body></html>";
            webView.loadData(summary, "text/html", null);
            toast("No Internet Connection.");
        }
    }
    private void toast(String message) {
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
    }

    public void onBackPressed() {
        if(webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    private boolean isOnline() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        return (netInfo != null && netInfo.isConnected());
    }
    public void but(View v){

        webView.loadUrl("http://testsite.com/");
    }
}

2 个答案:

答案 0 :(得分:1)

如果网址更改,则使用带有一些正则表达式的shouldOverrideUrlLoading

所以

List<String> validUrls = new LinkedList<>();
validUrls.add("https://www\\.google\\.com/*");
validUrls.add("https://www\\.facebook\\.com/*");


@Override public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (isValidUrl(url)) {
        return false;
    }
    return true;
}

private boolean isValidUrl(String url) {
    for (String validUrl : validUrls) {
        Pattern pattern = Pattern.compile(validUrl, Pattern.MULTILINE);
        Matcher matcher = pattern.matcher(url);
        if (matcher.find()) {
            return true;
        }
    }
    return false;
}

将与任何www.google.com或facebook.com网址匹配

答案 1 :(得分:0)

您可以拦截从网页浏览中打开的网址,我不知道它是否适用于弹出窗口:

    WebViewClient client= new WebViewClient(){
        @Override 
        public boolean shouldOverrideUrlLoading(WebView  view, String  url){
           if (url.equals("popupURL"){ 
                return true; 
           }
            return false;
        } 
    } 

webView.setWebViewClient(client);