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

时间:2015-07-21 10:37:07

标签: android cordova android-webview cordova-5.0.0

我想用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插件管理器,因此我无法将插件添加到webview。哪种方法最好?

1 个答案:

答案 0 :(得分:0)

我在MainActivity中使用此覆盖方法覆盖了SystemWebViewClient

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