2015-07-21 28 views
5

Tôi muốn xây dựng một WebView tùy chỉnh với Cordova. Để làm điều này, tôi muốn ghi đè phương thức setWebChromeClient và setWebViewClient. Nhưng cho rằng tôi cần phải có một SystemWebViewClient mà đòi hỏi một SystemWebViewEngine, mà tôi không thể dường như nhận được vào thời điểm này. Heres hoạt động chính của tôiXây dựng WebView tùy chỉnh với Cordova 5.0 trong Android

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

Và webview tùy chỉnh của tôi:

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

có thể không thực sự có được một động cơ để các phương pháp có thể được ghi đè, vì vậy tôi vừa tạo ra một một cách nhanh chóng nhưng điều này tạo ra một số lỗi. Ngoài ra, tôi không thể truy cập Trình quản lý Plugin Cordova và do đó tôi không thể thêm plugin vào webview. Cách tốt nhất để làm điều này là gì?

+0

gì lỗi bạn đã nhận được? – JSON

Trả lời

0

Tôi SystemWebViewClient overrided với phương pháp overrided này trong 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; 
} 
Các vấn đề liên quan