Cordova 中建立与WebView原生对象通信的核心代码



通过reflection 机制获取"SendMessage"接口,并进行通信调用。


private void initReflection()
/*     */     {
/* 349 */       Object webViewObject = NativeToJsMessageQueue.this.webView;
/* 350 */       Class webViewClass = WebView.class;
/*     */       try {
/* 352 */         Field f = webViewClass.getDeclaredField("mProvider");
/* 353 */         f.setAccessible(true);
/* 354 */         webViewObject = f.get(NativeToJsMessageQueue.this.webView);
/* 355 */         webViewClass = webViewObject.getClass();
/*     */       }
/*     */       catch (Throwable localThrowable1) {}
/*     */      
/*     */       try
/*     */       {
/* 361 */         Field f = webViewClass.getDeclaredField("mWebViewCore");
/* 362 */         f.setAccessible(true);
/* 363 */         this.webViewCore = f.get(webViewObject);
/*     */        
/* 365 */         if (this.webViewCore != null) {
/* 366 */           this.sendMessageMethod = this.webViewCore.getClass().getDeclaredMethod("sendMessage", new Class[] { Message.class });
/* 367 */           this.sendMessageMethod.setAccessible(true);
/*     */         }
/*     */       } catch (Throwable e) {
/* 370 */         this.initFailed = true;
/* 371 */         Log.e("JsMessageQueue", "PrivateApiBridgeMode failed to find the expected APIs.", e);
/*     */       }
/*     */     }

你可能感兴趣的:(mobile)