2016-05-31 103 views
1

這個問題在這裏並不新鮮,但我仍然無法得到它出錯的地方。我只是想評估一些JavaScript。Android在調用API的WebView中調用JavaScript函數<= 18

我簡單的代碼看起來像:

WebView wv = new WebView(context); 
WebSettings settings = wv.getSettings(); 
settings.setJavaScriptEnabled(true); 

JavaBridgeToJS bridgeToJS = new JavaBridgeToJS(); 
wv.addJavascriptInterface(bridgeToJS, "javaCallback"); 

String src = "javascript: var result= 3 + 3; window.javaCallback.resultOfAdd(result)"; 

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){ 
     Log.d(TAG, "Newer than JellyBean."); 
     ValueCallback<String> result = new ValueCallback<String>() { 
      @Override 
      public void onReceiveValue(String value) { 
       Log.d(TAG, "Got in onReceiveValue: " + value); 
      } 
     }; 
     wv.evaluateJavascript(src, result); 
    }else { 
     Log.d(TAG, "Older than Kitkat"); 
     wv.loadUrl(src); 
    } 

public class JavaBridgeToJS { 
    @JavascriptInterface 
    public void resultOfAdd(String result) { 
     Log.d(TAG, "Here is: " + result); 
    } 
} 

這是工作好API 19+,即使沒有如果用的版本檢查(我的意思是與loadUrl以及評估JavaScript一起工作)

但是當我嘗試API 18,不調用java中的回調。 我已到目前爲止,我有沒有運氣環顧四周(remove line break,或quotes missing可能受感染,看着手機上),等等...)

。任何想法?


編輯

好吧,我加入的代碼在辦案JS裏面的錯誤,我已經錯過了什麼。

wv.setWebChromeClient(new CustomWebChromeClient()); 

public class CustomWebChromeClient extends WebChromeClient { 
    private CustomWebChromeClient() { 
     super(); 
    } 

    @Override 
    public boolean onConsoleMessage(ConsoleMessage consoleMessage) { 
     UILog.d(TAG, consoleMessage.message()); 
     return super.onConsoleMessage(consoleMessage); 
    } 
} 

,我和他驚訝:

E/dalvikvm: Could not find class 'android.webkit.PermissionRequest', referenced from method com.example.WebViewDoll$CustomWebChromeClient.access$super 
VFY: unable to resolve check-cast 1896 (Landroid/webkit/PermissionRequest;) in Lcom/example/WebViewDoll$CustomWebChromeClient; 
E/dalvikvm: Could not find class 'android.webkit.WebChromeClient$FileChooserParams', referenced from method com.example.WebViewDoll$CustomWebChromeClient.access$super 
VFY: unable to resolve check-cast 1899 (Landroid/webkit/WebChromeClient$FileChooserParams;) in Lcom/example/WebViewDoll$CustomWebChromeClient; 
E/dalvikvm: Could not find class 'android.webkit.PermissionRequest', referenced from method com.example.WebViewDoll$CustomWebChromeClient.access$super 

D/WebViewDoll: Uncaught TypeError: Cannot call method 'resultOfAdd' of undefined 
E/Web Console: Uncaught TypeError: Cannot call method 'resultOfAdd' of undefined at null:1 
+0

**更新**我做了新的項目,以防萬一,但仍然沒有。 我想這應該工作[github回購](https://github.com/ThinkDeeper/webview_api18) – ThinkDeep

回答

0

我沒有找到一個答案,爲什麼這是行不通的,但與此using js in webview的幫助下,我才得以解決方法...

我必須注入我的js到html頁面。所以,我的包裝以防萬一JS的樣子:和

String src = "<html><head><script type=\"text/javascript\">" + 
      "function myFunction()" + 
      "{" + 
      " javaCallback.resultOfAdd(3+3);" + 
      "}" + 
      "</script></head><body onload=\"myFunction()\"/>" + 
      "</html>"; 

代替wv.loadUrl我用:

wv.loadData(src, "text/html", "UTF-8"); 

這種組合具有相同的結果需要。