Building custom WebView With Cordova 5.0 in Androi

2019-06-22 02:08发布

问题:

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();
    }
}

And my custom webview:

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

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?

回答1:

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