1

Here is my situation:
I have converted an Android application to a Blackberry z10 application. Everthing works well except one, in my application the Java code communicates with Javascript and vice versa. In Android it's pretty easy.

myWebView.addJavascriptInterface( new JSCommunicator() , "JSCommunicator" );

and from Javascript call functions like JSCommunicator.foo() however it is not working on z10. Is there any way to fix this? Thank you for your help, and sorry for the bad language.

Dediqated
  • 901
  • 15
  • 35
Davut Özcan
  • 109
  • 13

2 Answers2

1

Ok I found the solution, maybe its not the best answer but it solves the problem and works well on z10 simulator.

Instead of using JavaScriptInterface class handle alert in android, in this way if you convert your aplication to blackberry app you can also handle alert and do what ever you want according to the message.

Ex:

 alert( 'onFoo' );

In android ;

myWebView.setWebChromeClient( new WebChromeClient(){
    public boolean onJsAlert( WebView view , String url , String message , android.webkit.JsResult result ){
         if(message.compareTo( "onFoo" ) == 0){
              foo();
              result.confirm();
    return true;
         }
         return false;
    }


}
);
Nate
  • 31,017
  • 13
  • 83
  • 207
Davut Özcan
  • 109
  • 13
0

Have you added to your JS interface the @JavascriptInterface ?

 public class JSCommunicator {
     @JavascriptInterface
     public void foo() {
     //...
     }
 }

From JavascriptInterface methods in WebViews must now be annotated

Beginning in Android 4.2, you will now have to explicitly annotate public methods with @JavascriptInterface in order to make them accessible from hosted JavaScript. Note that this also only takes effect only if you have set your app's minSdkVersion or targetSdkVersion to 17 or higher.

Add it and import android.webkit.JavascriptInterface

AlexBcn
  • 2,450
  • 2
  • 17
  • 28
  • I added the annotation like you said but sorry that did not solve the problem. – Davut Özcan Jun 25 '13 at 11:21
  • Post your code. There are many things to comment about...meanwhile check the answer I did a few weeks ago. Look if you call with `window.JSCommunicator.foo`, enable the JS execution on webview settings `setJavaScriptEnabled(true)`...http://stackoverflow.com/questions/16342100/android-hide-webview-until-javascript-is-done?answertab=votes#tab-top – AlexBcn Jun 25 '13 at 11:38