在 Android 中使用 Cordova 5.0 构建自定义 WebView

2024-01-03

我想用 Cordova 构建自定义 WebView。为此,我想重写 setWebChromeClient 和 setWebViewClient 方法。但为此我需要一个 SystemWebViewClient ,它需要一个 SystemWebViewEngine ,但目前我似乎无法获得它。这是我的主要活动

public class MyActivity extends CordovaActivity implements CordovaInterface {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        int threadSize = getResources().getInteger(R.integer.WIPThreadPoolSize);

        // Initiate Thread pool
        threadPool = new ThreadPoolExecutor(threadSize, threadSize, 10, TimeUnit.SECONDS, this.priorityQueue);

        initApplicationInformationManager();

        // initialize global variables
        GlobalApplicationVariables.initValues(this);

        isActivityReady = false;

        isActivityReady = false;

        setContentView(R.layout.main_layout);
        super.init();

    }


    protected CordovaWebView makeWebView() {

        SystemWebView webView = (SystemWebView) findViewById(R.id.customWebView);
       SystemWebViewEngine engine = new SystemWebViewEngine(webView);


        return new CordovaWebViewImpl(engine);
    }

   protected void createViews() {
        appView.getView().requestFocusFromTouch();
    }
}

和我的自定义网络视图:

@SuppressLint("SetJavaScriptEnabled")
public class CustomWebView extends SystemWebView {

    private DebugServiceClient dbgClient;

    public CustomWebView(Context context) {
        super(context);

        this.configureView(context);
    }

   public CustomWebView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.configureView(context);
    }

    /**
     * Configure the web view.
     *
     * @param context
     *            Context web view is running in.
     */
    private void configureView(Context context) {
        //Make the WebView visible and hide its scroll bars
        setVisibility(View.VISIBLE);
        setVerticalScrollBarEnabled(false);
        setHorizontalScrollBarEnabled(false);
        setOverScrollMode(OVER_SCROLL_NEVER);

        WebSettings webSettings = getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportZoom(false);
        webSettings.setAppCacheEnabled(false);
        webSettings.setDomStorageEnabled(true);


        final MyActivity myActivity = (MyActivity) context;


        setWebChromeClient(new SystemWebChromeClient(new SystemWebViewEngine(this)) {

            public void onShowCustomView(View view, CustomViewCallback callback) {
           /*Custom code*/
            }

            public void onHideCustomView() {

            /*Custom code*/

            }
        });

        setWebViewClient(new SystemWebViewClient(new SystemWebViewEngine(this)) {

            public boolean shouldOverrideUrlLoading(WebView webview, String url) {
            /*Custom code*/    
            }

            public void onReceivedError(WebView view, int errorCod, String description, String failingUrl) {
                Log.e("Webview Error", errorCod + " - " + description + failingUrl);
            }
        }); 
    }        
}

无法真正获得一个引擎来覆盖这些方法,所以我只是即时创建了一个引擎,但这会产生一些错误。此外,我似乎无法访问 Cordova 插件管理器,因此我无法将插件添加到 web 视图中。哪种方法最好?


我在 MainActivity 中使用此重写方法重写了 System WebViewClient

@Override
protected CordovaWebViewEngine makeWebViewEngine() {
    CordovaWebViewEngine ret = CordovaWebViewImpl.createEngine(this, preferences);
    ((SystemWebView)ret.getView()).setWebViewClient(new SystemWebViewClient((SystemWebViewEngine)ret){
        @Override
        public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
            LOG.d(TAG, "ON RECEIVED SSL ERROR");
            super.onReceivedSslError(view, handler, error);
        }
    });
    return ret;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Android 中使用 Cordova 5.0 构建自定义 WebView 的相关文章

随机推荐

  • 获取 Magento 中的当前 URL 并显示一些内容

    我正在尝试获取 Magento 中的当前 URL 并显示一些内容 如果我当前位于该页面上 到目前为止 这就是我所做的并且有效 I am in the blog page 但是 我不想在源代码中硬编码URL 因为如果我传输到另一台服务器 我需
  • 写入 JSON 文件时出现阶段失败 Spark FileAlreadyExistsException

    我正在尝试以 JSON 格式将数据帧写入 s3 位置 但是每当执行器任务失败并且 Spark 重试该阶段时 它就会抛出FileAlreadyExistsException A 类似的问题 https stackoverflow com qu
  • 访问和使用数据库中的 .jsf 文件

    使我的 Web 应用程序能够使用数据库中存储的 JSF 文件的最佳方法是什么 我希望能够动态 在运行时 创建新的 JSF 页面 无需重新部署应用程序即可使用该页面 换句话说 我希望将 JSF 页面的大部分存储在数据库中 并希望 JSF 使用
  • 在 P/Invoke 中为固定字符串传递什么?

    假设这个 C 函数 void do something const char str 它将字符串存储在某处稍后参考 此外 我在 C 中有这个签名来调用这个函数 DllImport NativeLib static extern void d
  • Java 中的窗口拖动事件

    我正在尝试在 Java 中实现类似 OS X 抽屉的功能 因此我将一个窗口隐藏在另一个窗口下 但是 当我拖动主窗口 JFrame 时 我需要在它移动到下面的辅助窗口 JWindow 时发送更新 所以它会是这样的 JFrame JWidow
  • 使用 laravel forge 永远运行 artisan 命令?

    有人可以建议我如何让我的自定义 artisan 命令与守护进程一起永远运行吗 我看过很多关于队列的教程 但它并不完全适合 我正在尝试使用 pubnub 的 php 库完成 订阅 这似乎是最好的方法 除非我错过了一些东西 提前致谢 如果您从命
  • 如何使用 LDAP 组成员身份同步设置 TeamCity [关闭]

    Closed 这个问题是与编程或软件开发无关 help closed questions 目前不接受答案 这是一个简短的指南 帮助您进行 TeamCity 的 LDAP 设置 在我设法让同步运行之前 我个人经历了相当多的挣扎 默认配置文件有
  • 是否可以在 MATLAB 中实现以下内容?

    给定 5x5 的图像 I i j 4 i j 当 i j 0 1 2 3 4 时 以及以下 Prewitt 运算符 1 0 1 1 0 1 1 0 1 1 1 1 0 0 0 1 1 1 如何计算梯度 幅度和方向 使用 Prewitt 算子
  • ASP.NET Core - 如何注入 IOptionsMonitor

    我怎样才能注射IOptionsMonitor
  • 将 php 与 bigcommerce api 集成

    我是 Bigcommerce Api 整个概念的新手 但我的 php 编程很好 如果我要将我可能创建的 php 代码与 bigcommerce api 集成 我无法确定从哪里开始 我的文件去哪里 他们具体是如何互动的 在 Bigcommer
  • 默认路由总是在react router中执行

    我正在开发一个项目 其中使用了引人注目的Dash 模板 在这里 我在从 URL 更改路由时遇到一些路由问题 auth js import React lazy Suspense from react import Spin from ant
  • CancellationToken 泄漏内存

    我有一个计时器 每 2 秒启动 2 个任务 我在一个简单的列表中跟踪这些任务 这样我可以在停止应用程序时等待它们完成 这些任务有效地进入数据库 运行几次更新并完成 任务本身的运行时间不会超过一秒 global variables to ke
  • 使用点击数据更新绘图[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我正在尝试构建一个闪亮的应用程序 它允许用户单击绘图上的多个位置 然后单击按钮将这些坐标添加到绘图中 问题是我需要将这些坐标作为向量传
  • Haystack Faceted:__init__() 得到了意外的关键字参数“facet_fields”

    在享受 haystack 2 4 1 Django 1 8 的第一个成果的同时 我不得不承认我在学习它时遇到了困难 文档有时不完整 并且某些功能只有很少的示例 分面搜索就是其中之一 我正在关注文档 http django haystack
  • 如何使用openCV保存裁剪后的图像?

    我正在使用 python 中的 opencv 和face recognition 库捕获图像 这是我的程序的片段 while True ret frame video capture read rgb frame frame 1 face
  • Javascript平方数组中的所有元素不起作用

    function square arr var result concat arr result forEach function i i i i console log i return result var arr 1 2 3 4 co
  • BlueGiga BLE 开发工具

    我正在考虑尝试BlueGiga BLE112 套件 https www bluegiga com en US products bluetooth 4 0 modules ble112 bluetooth smart module 为 iO
  • 如何更改 DRF 中的验证错误响应?

    我想更改 JSON 当发生验证错误时 rest framework 或 Django 返回该 JSON 我将使用我的视图之一作为示例 但我想更改所有视图的错误消息 假设我有这个视图 旨在登录用户 提供电子邮件和密码 如果这些正确 它将返回
  • 使用 ggplot 堆积条形图

    我正在尝试使用以下数据框制作堆积条形图 totalleft 1S 2S 3S 4S 12S 25S tests A 000 5 0 10 10 0 NA A 000 A 001 10 8 10 NA NA NA A 001 A 002 5
  • 在 Android 中使用 Cordova 5.0 构建自定义 WebView

    我想用 Cordova 构建自定义 WebView 为此 我想重写 setWebChromeClient 和 setWebViewClient 方法 但为此我需要一个 SystemWebViewClient 它需要一个 SystemWebV