在Android中使用Cordova 5.0构建自定义WebView [英] Building custom WebView With Cordova 5.0 in Android

查看:329
本文介绍了在Android中使用Cordova 5.0构建自定义WebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Cordova构建自定义WebView.为此,我想重写setWebChromeClient和setWebViewClient方法.但是为此,我需要有一个SystemWebViewClient,它需要一个SystemWebViewEngine,我现在似乎无法理解.这是我的主要活动

I want to build a custom WebView with Cordova. To do this I want to override setWebChromeClient and the setWebViewClient methods. But for that I need to have a SystemWebViewClient which requires a SystemWebViewEngine, which I cant seem to get at this point. Heres my main activity

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插件管理器,因此无法将插件添加到Webview.哪个是最好的方法?

Can't really get an engine so that the methods can be overwritten, so I just created one on the fly but this generates some errors. Additionally I cant seem to access the Cordova Plugin Manager and consequently I'm not able to add plugins to the webview. Which is the best way to do this?

推荐答案

我在MainActivity中使用此重写的方法重写了SystemWebViewClient

I'm overrided SystemWebViewClient with this overrided method in MainActivity

@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;
}

这篇关于在Android中使用Cordova 5.0构建自定义WebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆