【发布时间】:2016-02-12 07:44:49
【问题描述】:
我的代码遇到了一个特殊问题。 javascript 回调在 api 级别 19 或更低的设备上运行,但它不适用于 API 21 及更高版本。奇怪的正常 Html javascript 回调适用于所有 API 级别。我没有使用proguard。这是我的代码:
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 代码:
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);
}-*/;
我在棒棒糖及更高版本上将 typeof Android 设置为“未定义”。谁能告诉我这有什么问题,或者给我一个使用 GWT javascript 的示例代码,该代码适用于 Android API 级别 21 及更高级别。
【问题讨论】:
-
可能是竞争条件?您可能想将其放入 Timer 以确保它不是。
-
我尝试添加 setTimeout(function () {$wnd.setHeading(heading);}, 3000);它奏效了!!非常感谢!
-
我已将其添加为答案。请接受它:)
标签: javascript java android gwt android-webview