在 Android 中定义和设置自定义 WebViewClient class 的选项

Define and set options for custom WebViewClient class in Android

我创建了一个自定义 WebViewClient class,它的交互非常有限 - 它只能加载亚马逊 URL,不允许点击链接。 (允许 amzn.tosmile.amazon 重定向)

package com.domain.mypackage;

import android.net.Uri;
import android.view.KeyEvent;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class nonInteractiveWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideKeyEvent (WebView view, KeyEvent event) {
        return true;
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        String host = Uri.parse(view.getUrl()).getHost();

        if ("amzn.to".equals(host)) {
            return false;
        } else if (host != null && host.matches("(?i)^smile\.amazon.*")) {
            return false;
        } else {
            return true;
        }
    }
}

我想增强这个新的 class,这样如果我启用一个选项,它 允许点击链接,但只允许点击 Amazon 链接。因此,我添加了一个布尔值,希望能够通过调用 Activity.

进行设置
package com.domain.mypackage;

import android.net.Uri;
import android.view.KeyEvent;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class nonInteractiveWebViewClient extends WebViewClient {

    public boolean canBrowseAmazonSites = false;

    @Override
    public boolean shouldOverrideKeyEvent (WebView view, KeyEvent event) {
        return true;
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        String host = Uri.parse(view.getUrl()).getHost();

        if ("amzn.to".equals(host)) {
            return false;
        } else if (host != null && host.matches("(?i)^smile\.amazon.*")) {
            return false;
        // Allow clicking of Amazon links
        } else if ( canBrowseAmazonSites && host != null && ( host.matches("(?i)^smile\.amazon.*") || host.matches("(?i)^www\.amazon.*") ) ) {
            return false;
        } else {
            return true;
        }
    }
}

我在调用 Activity 中创建 WecViewClient 如下:

    viewBinding.amazonWebview.getSettings().setJavaScriptEnabled(true);
    viewBinding.amazonWebview.getSettings().setSupportZoom(true);
    viewBinding.amazonWebview.setWebViewClient(new nonInteractiveWebViewClient() {
        @Override
        public void onPageStarted(WebView _param1, String _param2, Bitmap _param3) {
            super.onPageStarted(_param1, _param2, _param3);
            viewBinding.progressBar.setIndeterminate(true);
        }

        @Override
        public void onPageFinished(WebView _param1, String _param2) {
            super.onPageFinished(_param1, _param2);
            viewBinding.progressBar.setIndeterminate(false);
        }

    });

但我不知道如何从 Activity.

中设置我的布尔值

我确实想知道布尔值是否是正确的方法,或者我是否应该创建一个 public 方法,我可以调用它来设置布尔值,但无论哪种方式,我都不知道如何访问布尔值或方法。

我想我已经解决了,但是请评论我所做的是否有任何问题。

我已经按照问题留下了 class 代码,但添加到主代码中 activity。

Class 代码因此保持:

package com.domain.mypackage;

import android.net.Uri;
import android.view.KeyEvent;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class nonInteractiveWebViewClient extends WebViewClient {

    public boolean canBrowseAmazonSites = false;

    @Override
    public boolean shouldOverrideKeyEvent (WebView view, KeyEvent event) {
        return true;
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        String host = Uri.parse(view.getUrl()).getHost();

        if ("amzn.to".equals(host)) {
            return false;
        } else if (host != null && host.matches("(?i)^smile\.amazon.*")) {
            return false;
        // Allow clicking of Amazon links
        } else if ( canBrowseAmazonSites && host != null && ( host.matches("(?i)^smile\.amazon.*") || host.matches("(?i)^www\.amazon.*") ) ) {
            return false;
        } else {
            return true;
        }
    }
}

但现在当我将 WebViewClient 分配给 Activity 中的 WebView 时,我用对象名称声明它(而不是匿名?)。

然后我可以从该命名对象访问布尔值,如下所示:

   viewBinding.amazonWebview.getSettings().setJavaScriptEnabled(true);
    viewBinding.amazonWebview.getSettings().setSupportZoom(true);
    viewBinding.amazonWebview.setWebViewClient(amazonWebViewClient = new nonInteractiveWebViewClient() {
        @Override
        public void onPageStarted(WebView _param1, String _param2, Bitmap _param3) {
            super.onPageStarted(_param1, _param2, _param3);
            viewBinding.progressBar.setIndeterminate(true);
        }

        @Override
        public void onPageFinished(WebView _param1, String _param2) {
            super.onPageFinished(_param1, _param2);
            viewBinding.progressBar.setIndeterminate(false);
        }
    });

    amazonWebViewClient.canBrowseAmazonSites = true;

其中amazonWebViewClient在Activityclass中贴标如下:

    nonInteractiveWebViewClient amazonWebViewClient;