我正在尝试与 JS api 交互,一旦视频加载/加载失败/被查看,我想使用 EventSystem 发回一个事件。我得到了第一部分的工作,我称之为原生 JS api:
@JsType(namespace = JsPackage.GLOBAL, name = "ApiRequestHandler", isNative = true)
public class ApiRequestHandler {
public native void loadVideo();
public native void showVideo();
}
ApiRequestHandler= function () {
var preloadedVideo = null;
}
ApiRequestHandler.prototype.loadVideo = function () {
Api.getVideoAsync().then(function(video) {
// Load asynchronously
preloadedVideo = video;
return preloadedVideo.loadAsync();
}).then(function() {
console.log('Video preloaded');
}).catch(function(err){
console.error('Video failed to preload: ' + err.message);
});
}
ApiRequestHandler.prototype.showVideo = function () {
preloadedVideo.showAsync()
.then(function() {
// Perform post-ad success operation
console.log('Video watched successfully');
})
.catch(function(e) {
console.error(e.message);
});
}
我的问题是从 JS 调用 Java 对象,我附带了一个响应处理程序类,它依赖于 EventSystem。如何在此类中初始化 EventSystem,然后让 JS 承诺解析调用sendEvent
?(为了说明清楚,上面的JS代码控制台调用,需要改为调用eventSystem,来报告成功或错误)。
@JsType(isNative = true)
public class ApiResponseHandler {
EventSystem eventSystem;
public void sendEvent(int msg) {
final Event event = new Event();
event.message = msg;
eventSystem.dispatch(event);
}
@JsOverlay
public void setEventSystem(EventSystem eventSystem) {
this.eventSystem = eventSystem;
}
}