Issue
I am facing a particular problem with my code. The javascript callbacks are working on devices with api level 19 or lower but it is not working on API 21 and above. Strangely normal Html javascript callback is working on all API levels. I'm not using proguard. Here's my code:
MainActivity.java
WebAppInterface wai = new WebAppInterface(this, this);
myWebView.addJavascriptInterface(wai, "Android");
myWebView.loadUrl("http://192.168.2.246/abhishek/test/");
WebAppInterface.java
class WebAppInterface {
Context mContext;
MainActivity parent;
/** Instantiate the interface and set the context */
WebAppInterface(Context c, MainActivity parent) {
mContext = c;
this.parent = parent;
}
@JavascriptInterface
public void setHeading(String heading) {
Toast.makeText(mContext, "Setting heading to " + heading, Toast.LENGTH_SHORT).show();
}
}
GWT code:
javascript:
function setHeading(heading) {
console.log("setting heading...");
// Android callback
if(typeof Android !== 'undefined'){
Android.setHeading(heading);
}
if(typeof Android == 'undefined') {
console.log("undefined!!");
}
}
java:
public static final native void setAppHeading(String heading) /*-{
$wnd.setHeading(heading);
}-*/;
I am getting typeof Android as 'undefined' on lollipop and above. Can anyone give tell me what's wrong in this or give me a sample code with GWT javascript that works on Android API level 21 and above.
Solution
The timeout delay seems to solve the issue. I don't know how but it works.
public static final native void setAppHeading(String heading) /*-{
setTimeout(function() {$wnd.setHeading(heading);}, 3000);
}-*/;
Turns out the callback object was attached at the window level and javascript code was executed in the inner iFrame created by GWT.
public static final native void setAppHeading(String heading) /*-{
if(typeof $wnd.Android !== 'undefined'){
$wnd.Android.setHeading(heading);
return;
}
}-*/;
So this code works.
Answered By - user1608639
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.