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

joa*_*rso 5 android android-webview cordova 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

我的自定义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);
            }
        }); 
    }        
}
Run Code Online (Sandbox Code Playgroud)

无法真正获得引擎以便可以覆盖这些方法,因此我只是动态创建了一个引擎,但这会产生一些错误.另外,我似乎无法访问Cordova插件管理器,因此我无法将插件添加到webview.这是最好的方法吗?

Ele*_*ant 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;
}
Run Code Online (Sandbox Code Playgroud)